ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

Discussion of Problem 1247. Check a Sequence

O(n) Solution
Posted by Aditya Singh 11 Dec 2017 08:13
Subtract 1 from each element
Apply window to the array for max sum of S


#include <bits/stdc++.h>
using namespace std;
#define repl(i,x,n) for(long long i=(long long)(x);i<(long long)(n);i++)
#define rep(i,x,n) for(long i=long(x);i<long(n);i++)
int main()
{
    //ifstream cin("input.in");
    //ofstream cout("output.out");
    ios::sync_with_stdio(0);cin.tie(0);
    long n,s,a[100009],p=0;
    cin>>n>>s;
    bool flag=1;
    rep(i,0,n)
    {
        cin>>a[i];
        a[i]--;
    }
    rep(i,0,n)
    {
        p+=a[i];
        if(p>s)
            flag=0;
        p=max(p,0l);
    }
    if(flag)
        cout<<"YES";
    else
        cout<<"NO";
}

Edited by author 11.12.2017 08:23