Submission #396377

#TimeUsernameProblemLanguageResultExecution timeMemory
396377jeroenodbDivide and conquer (IZhO14_divide)C++14
100 / 100
43 ms1864 KiB
#include "bits/stdc++.h"
using namespace std;
#define all(x) begin(x),end(x)
template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; }
template<typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type> ostream& operator<<(ostream &os, const T_container &v) { string sep; for (const T &x : v) os << sep << x, sep = " "; return os; }
#define debug(a) cerr << "(" << #a << ": " << a << ")\n";
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> pi;
const int mxN = 1e5+1, oo = 1e9;
struct el {
    ll pref, gold;
    bool bad(const el& nw) const {
        return pref <= nw.pref;
    }
};
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int n; cin >> n;
    ll sum = 0,pref=0, ans = 0;
    vector<el> st; st.reserve(n);
    for(int i=0;i<n;++i) {
        int x,g,d; cin >> x >> g >> d;


        el nw = {pref-x,sum};
        if(st.empty() or !st.back().bad(nw))
            st.push_back(nw);
        int l=0,r=st.size();
        while(l<r) {
            int mid = (l+r)/2;
            if(st[mid].pref<=pref+d-x) {
                r = mid;
            } else {
                l = mid+1;
            }
        }
        if(l!=(int)st.size()) {
            ans = max(sum+g-st[l].gold, ans);
        }
        pref += d;
        sum+=g;
    }
    cout << ans << '\n';
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...