Skip to content

Instantly share code, notes, and snippets.

@enzerr
Created May 31, 2023 14:42

Revisions

  1. enzerr created this gist May 31, 2023.
    35 changes: 35 additions & 0 deletions Danoninho2.cpp
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,35 @@
    #include<bits/stdc++.h>
    using namespace std;
    #define int long long
    const int maxn = 1e5 + 5;

    int v[maxn];
    int n, D;

    bool check(int x){
    bool rt = false;

    int val = 0;
    for(int i = 0; i < x; i++) val += v[i];
    for(int i = x; i<n; i++){
    if(val<=D) rt = true;
    val += v[i] - v[i-x];
    }
    if(val<=D) rt = true;

    return rt;
    }

    int32_t main(){
    cin >> n >> D;
    for(int i = 0; i < n; i++) cin >> v[i];

    int l = 0, r = n;
    while(l<=r){
    int mid = (l+r)/2;
    if(check(mid)) l = mid+1;
    else r = mid-1;
    }

    cout << l-1 << '\n';
    }