Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- int a[100005];
- int n;
- int pref_sum[100005];
- int sum(int i, int j) {
- if(i == 0) {
- return pref_sum[j];
- }
- return pref_sum[j] - pref_sum[i - 1];
- }
- int main() {
- int m;
- cin >> n >> m;
- int s = 0;
- for(int i = 0; i < n; i++) {
- cin >> a[i];
- s += a[i];
- pref_sum[i] = s;
- }
- int ret = 0;
- for(int i = 0; i < n; i++) { // probuvame da pocneme od sekoja pozicija
- int L = i + 1;
- int R = n - 1;
- while(L <= R) {
- int middle = (L + R) / 2;
- if(sum(i, middle) <= m) {
- ret = max(ret, middle - i + 1);
- L = middle + 1;
- }
- else {
- R = middle - 1;
- }
- }
- }
- cout << ret << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement