Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <vector>
- #include <set>
- #include <map>
- #include <iostream>
- using namespace std;
- const int maxn = 2e5 + 10;
- int pref_sum[maxn];
- int query(int i, int j) {
- if(i == 0) {
- return pref_sum[j];
- }
- return pref_sum[j] - pref_sum[i - 1];
- }
- int main() {
- int n, k;
- cin >>n >> k;
- vector<int> v(n);
- int sum = 0;
- for(int i = 0; i < n; i++) {
- cin >> v[i];
- sum += v[i];
- pref_sum[i] = sum;
- }
- int res = 0;
- int S = 0;
- for(int i = 0; i < n; i++) {
- int L = i;
- int R = n - 1;
- int idx = i;
- while(L <= R) {
- int mid = (L + R) / 2;
- int sum = query(i, mid);
- if(sum <= k) {
- L = mid + 1;
- idx = max(idx, mid);
- }
- else {
- R = mid - 1;
- }
- }
- if(idx - i + 1 > res) {
- res = idx - i + 1;
- }
- }
- cout << res << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement