Untitled

mail@pastecode.io avatar
unknown
c_cpp
3 years ago
937 B
4
Indexable
Never
#include <iostream>
#include <queue>
#include <algorithm>
using namespace std;
const int maxn = 100005;
int n;
long long x;
int niza[maxn];
long long pref_sum[maxn];
long long suma(int i, int j) {
    if(i == 0) {
        return pref_sum[j];
    }
    return pref_sum[j] - pref_sum[i - 1];
}
int main()
{
    cin >> n >> x;
    long long sum = 0;
    for(int i = 0; i < n; i++) {
        cin >> niza[i];
        sum += niza[i];
        pref_sum[i] = sum;
    }
    int najdolga = 0;

    for(int i = 0; i < n; i++) {
        int L = i + 1;
        int R = n - 1;
        while(L <= R) {
            int mid = (L + R) / 2;
            sum = suma(i, mid);
            if(sum > x) {
                R = mid - 1;
            }
            else {
                najdolga = max(najdolga, mid - i + 1);
                L = mid + 1;
            }
        }
    }
    cout << najdolga << endl;
    return 0;
}

/*
 6 50
 12 13 14 12 11 11
 
 */