This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
const int N = 200005;
int n;
long long x[N], g[N], d[N];
long long T[N];
long long ans;
vector<long long> z; // for compression
void upd(int x, long long val) { for(; x < N; x += x & -x) T[x] = max(T[x], val); }
long long get(int x) { long long res = 0; for (; x > 0; x -= x & -x) res = max(res, T[x]); return res; }
int main() {
ios_base::sync_with_stdio(false); cin.tie(0);
cin >> n;
for (int i = 1; i <= n; ++i) cin >> x[i] >> g[i] >> d[i], d[i] += d[i-1]; // (coordinate, gold, energy)
for (int i = 1; i <= n; ++i) {
// compress all values
z.push_back(d[i] - x[i]);
z.push_back(d[i-1] - x[i]);
}
sort(z.begin(), z.end());
z.resize(distance(z.begin(), unique(z.begin(), z.end())));
for (int i = 1; i <= n; ++i) {
long long dp = g[i];
int pos = lower_bound(z.begin(), z.end(), d[i] - x[i]) - z.begin() + 1;
dp += get(pos);
pos = lower_bound(z.begin(), z.end(), d[i-1] - x[i]) - z.begin() + 1;
upd(pos, dp);
ans = max(ans, dp);
}
cout << ans << endl;
}
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |