#include "bits/stdc++.h"
// @JASPER'S BOILERPLATE
using namespace std;
using ll = long long;
#ifdef JASPER
#include "debug.h"
#else
#define debug(...) 166
#endif
const int N = 2e5 + 5;
template <typename T>
struct FenwickTree {
vector <T> f;
int n;
const T INF = 1e18 + 5;
FenwickTree(int _n){
init(_n);
}
void init(int _n) {
n = _n;
f.assign(n + 5, INF);
}
void upd(int x, ll val) {
for (; x <= n; x += x & (-x))
f[x] = min(f[x], val);
}
T qry(int x) {
T ans = INF;
for (; x > 0; x -= x & (-x))
ans = min(ans, f[x]);
return ans;
}
};
int n;
int x[N], g[N], e[N];
ll prfE[N], prfG[N], dp[N];
signed main() {
cin.tie(0) -> sync_with_stdio(0);
cin >> n;
for (int i = 1; i <= n; ++i) cin >> x[i] >> g[i] >> e[i];
vector <ll> vals;
for (int i = 1; i <= n; ++i) {
prfG[i] = prfG[i - 1] + g[i];
prfE[i] = prfE[i - 1] + e[i];
vals.push_back(prfE[i] - x[i]);
vals.push_back(prfE[i - 1] - x[i]);
// xi - xj <= fe(i) - fe(j - 1) -> fe(i) - xi >= fe(j - 1) - xj
// dp(i) = dp(j) + f(j + 1, i) = f(i) + max(dp(j) - f(j));
}
sort(vals.begin(), vals.end());
vals.resize(unique(vals.begin(), vals.end()) - vals.begin());
auto get = [&] (ll tar) {
return (int) (lower_bound(vals.begin(), vals.end(), tar) - vals.begin() + 1);
};
FenwickTree <ll> fen((int) vals.size());
for (int i = 1; i <= n; ++i) {
ll p = get(prfE[i] - x[i]);
dp[i] = max(1LL * g[i], prfG[i] - fen.qry(p));
ll _p = get(prfE[i - 1] - x[i]);
fen.upd(_p, prfG[i - 1]);
}
cout << (*max_element(dp + 1, dp + 1 + n)) << "\n";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2524 KB |
Output is correct |
7 |
Correct |
1 ms |
2392 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2516 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2524 KB |
Output is correct |
7 |
Correct |
1 ms |
2392 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2516 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
1 ms |
2396 KB |
Output is correct |
18 |
Correct |
1 ms |
2524 KB |
Output is correct |
19 |
Correct |
1 ms |
2396 KB |
Output is correct |
20 |
Correct |
1 ms |
2396 KB |
Output is correct |
21 |
Correct |
1 ms |
2396 KB |
Output is correct |
22 |
Correct |
1 ms |
2652 KB |
Output is correct |
23 |
Correct |
2 ms |
2876 KB |
Output is correct |
24 |
Correct |
3 ms |
2908 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2524 KB |
Output is correct |
7 |
Correct |
1 ms |
2392 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2516 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
1 ms |
2396 KB |
Output is correct |
18 |
Correct |
1 ms |
2524 KB |
Output is correct |
19 |
Correct |
1 ms |
2396 KB |
Output is correct |
20 |
Correct |
1 ms |
2396 KB |
Output is correct |
21 |
Correct |
1 ms |
2396 KB |
Output is correct |
22 |
Correct |
1 ms |
2652 KB |
Output is correct |
23 |
Correct |
2 ms |
2876 KB |
Output is correct |
24 |
Correct |
3 ms |
2908 KB |
Output is correct |
25 |
Correct |
2 ms |
2908 KB |
Output is correct |
26 |
Correct |
6 ms |
3296 KB |
Output is correct |
27 |
Correct |
5 ms |
3264 KB |
Output is correct |
28 |
Correct |
20 ms |
6516 KB |
Output is correct |
29 |
Correct |
24 ms |
6604 KB |
Output is correct |
30 |
Correct |
46 ms |
11168 KB |
Output is correct |
31 |
Correct |
36 ms |
10192 KB |
Output is correct |
32 |
Correct |
37 ms |
10484 KB |
Output is correct |
33 |
Correct |
38 ms |
10440 KB |
Output is correct |
34 |
Correct |
39 ms |
9832 KB |
Output is correct |
35 |
Correct |
37 ms |
10584 KB |
Output is correct |
36 |
Correct |
39 ms |
10568 KB |
Output is correct |