Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- int pref_sum[100005];
- int suma(int i, int j) {
- if(i == 0) {
- return pref_sum[j];
- }
- return pref_sum[j] - pref_sum[i - 1];
- }
- int main() {
- int n;
- cin >> n;
- int m;
- cin >> m;
- int niza[n];
- int sum = 0;
- for(int i = 0; i < n; i++) {
- cin >> niza[i];
- sum += niza[i];
- pref_sum[i] = sum;
- }
- int res = 0;
- for(int i = 0; i < n; i++) {
- int L = 0, R = n - 1;
- int idx = -1;
- while(L <= R) {
- int mid = (L + R) / 2;
- int kolku_e_sumata = suma(i, mid);
- if(kolku_e_sumata <= m) {
- if(idx < mid) {
- idx = mid;
- }
- L = mid + 1;
- }
- 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