#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pb push_back
#define sz(x) (int)(x.size())
#define itr(x) x.begin(), x.end()
#define ref(x) (*(x))
#define prv(x) for(auto& pval : x) cout << pval << " "; cout << "\n";
#ifdef LOC
#define debug(...) cerr << #__VA_ARGS__ << " : "; for(auto& dval : {__VA_ARGS__}) cerr << dval << " "; cerr << "\n";
#else
#define debug(...)
#endif
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
template<typename T>
ostream& operator << (ostream& out, vector<T> v) {
out << "{ ";
for(auto& i : v) {
out << i << " ";
}
out << "} ";
return out;
}
long long max_weights(int N, int M, std::vector<int> X, std::vector<int> Y, std::vector<int> W) {
const ll maxh = N + 5;
vector<vector<array<ll,2>>> xpoints(N);
for(ll i = 0; i < M; i++) {
xpoints[X[i]].pb({Y[i] + 1, W[i]});
}
for(auto& v : xpoints) {
sort(itr(v));
}
vector<vector<ll>> pfx(N);
vector<vector<ll>> dp_idxs(N);
for(ll x = 0; x < N; x++) {
if(sz(xpoints[x]) == 0 || xpoints[x][0][0] > 1) {
pfx[x].pb(0);
dp_idxs[x].pb(0);
}
for(auto& p : xpoints[x]) {
if(p[0] != 0 && (!sz(dp_idxs[x]) || dp_idxs[x].back() != p[0] - 1)) {
pfx[x].pb((sz(pfx[x]) ? pfx[x].back() : 0));
dp_idxs[x].pb(p[0] - 1);
}
if(!sz(dp_idxs[x]) || dp_idxs[x].back() != p[0]) {
pfx[x].pb((sz(pfx[x]) ? pfx[x].back() : 0) + p[1]);
dp_idxs[x].pb(p[0]);
}
}
pfx[x].pb((sz(pfx[x]) ? pfx[x].back() : 0));
dp_idxs[x].pb(maxh);
}
debug(pfx);
debug(dp_idxs);
vector<vector<ll>> inc_dp(N), dec_dp(N);
inc_dp[0].assign(sz(dp_idxs[0]), 0);
dec_dp[0].assign(sz(dp_idxs[0]), 0);
for(ll x = 1; x < N; x++) {
ll psz = sz(dp_idxs[x - 1]), csz = sz(dp_idxs[x]);
// calc inc_dp
inc_dp[x].assign(sz(dp_idxs[x]), 0);
ll gmx = 0; ll gptr = psz - 1;
for(ll i = csz - 1; i >= 0; i--) {
ll h = dp_idxs[x][i];
while(gptr >= 0 && dp_idxs[x - 1][gptr] >= h) {
ll v = max(inc_dp[x - 1][gptr], dec_dp[x - 1][gptr]);
gmx = max(gmx, v + pfx[x][i]);
gptr--;
}
inc_dp[x][i] = max(inc_dp[x][i], gmx - pfx[x][i]);
}
dec_dp[x].assign(csz, 0);
ll smx = 0; ll sptr = 0;
for(ll i = 0; i < csz; i++) {
ll h = dp_idxs[x][i];
while(sptr < psz && dp_idxs[x - 1][sptr] <= h) {
ll v = dec_dp[x - 1][sptr];
smx = max(smx, v - pfx[x - 1][sptr]);
sptr++;
}
dec_dp[x][i] = max(dec_dp[x][i], smx + pfx[x - 1][sptr - 1]);
}
if(x == 1) {
continue;
}
ll ppsz = sz(dp_idxs[x - 2]);
ll gpptr = ppsz - 1; gptr = psz - 1;
ll gppmx = 0, gvmx = 0;
for(ll i = csz - 1; i >= 0; i--) {
ll h = dp_idxs[x][i];
while(gptr >= 0 && dp_idxs[x - 1][gptr] >= h) {
while(gpptr >= 0 && dp_idxs[x - 2][gpptr] >= dp_idxs[x - 1][gptr]) {
gppmx = max(gppmx, dec_dp[x - 2][gpptr]);
gppmx = max(gppmx, inc_dp[x - 2][gpptr]);
gpptr--;
}
gvmx = max(gvmx, pfx[x - 1][gptr] + gppmx);
gptr--;
}
dec_dp[x][i] = max(dec_dp[x][i], gvmx);
}
ll spptr = 0; sptr = 0;
ll sppmx = 0;
for(ll i = 0; i < csz; i++) {
ll h = dp_idxs[x][i];
while(spptr < ppsz && dp_idxs[x - 2][spptr] <= h) {
sppmx = max(sppmx, dec_dp[x - 2][spptr]);
sppmx = max(sppmx, inc_dp[x - 2][spptr]);
spptr++;
}
while(sptr + 1 < psz && dp_idxs[x - 1][sptr + 1] <= h) {
sptr++;
}
dec_dp[x][i] = max(dec_dp[x][i], sppmx + pfx[x - 1][sptr]);
}
}
debug(inc_dp);
debug(dec_dp);
ll ans = 0;
for(auto& p : inc_dp[N - 1]) {
ans = max(ans, p);
}
for(auto& p : dec_dp[N - 1]) {
ans = max(ans, p);
}
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
74 ms |
28092 KB |
Output is correct |
2 |
Correct |
92 ms |
31524 KB |
Output is correct |
3 |
Correct |
42 ms |
24572 KB |
Output is correct |
4 |
Correct |
40 ms |
24504 KB |
Output is correct |
5 |
Correct |
171 ms |
46884 KB |
Output is correct |
6 |
Correct |
272 ms |
61280 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
107 ms |
33980 KB |
Output is correct |
3 |
Correct |
127 ms |
38736 KB |
Output is correct |
4 |
Correct |
71 ms |
28060 KB |
Output is correct |
5 |
Correct |
82 ms |
31532 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
40 ms |
24524 KB |
Output is correct |
11 |
Correct |
41 ms |
24516 KB |
Output is correct |
12 |
Correct |
77 ms |
29548 KB |
Output is correct |
13 |
Correct |
98 ms |
33284 KB |
Output is correct |
14 |
Correct |
70 ms |
29660 KB |
Output is correct |
15 |
Correct |
78 ms |
32848 KB |
Output is correct |
16 |
Correct |
90 ms |
29632 KB |
Output is correct |
17 |
Correct |
105 ms |
32776 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
40 ms |
24564 KB |
Output is correct |
2 |
Correct |
41 ms |
24528 KB |
Output is correct |
3 |
Correct |
70 ms |
27596 KB |
Output is correct |
4 |
Correct |
62 ms |
28192 KB |
Output is correct |
5 |
Correct |
125 ms |
34804 KB |
Output is correct |
6 |
Correct |
102 ms |
34088 KB |
Output is correct |
7 |
Correct |
98 ms |
34744 KB |
Output is correct |
8 |
Correct |
112 ms |
34748 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
2 ms |
576 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
2 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
304 KB |
Output is correct |
14 |
Correct |
1 ms |
440 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
2 ms |
576 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
2 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
304 KB |
Output is correct |
14 |
Correct |
1 ms |
440 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
2 ms |
468 KB |
Output is correct |
17 |
Correct |
19 ms |
5460 KB |
Output is correct |
18 |
Correct |
18 ms |
5556 KB |
Output is correct |
19 |
Correct |
19 ms |
5284 KB |
Output is correct |
20 |
Correct |
20 ms |
5332 KB |
Output is correct |
21 |
Correct |
21 ms |
5308 KB |
Output is correct |
22 |
Correct |
40 ms |
10300 KB |
Output is correct |
23 |
Correct |
5 ms |
1588 KB |
Output is correct |
24 |
Correct |
15 ms |
4180 KB |
Output is correct |
25 |
Correct |
1 ms |
468 KB |
Output is correct |
26 |
Correct |
5 ms |
1336 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
2 ms |
576 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
2 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
304 KB |
Output is correct |
14 |
Correct |
1 ms |
440 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
2 ms |
468 KB |
Output is correct |
17 |
Correct |
19 ms |
5460 KB |
Output is correct |
18 |
Correct |
18 ms |
5556 KB |
Output is correct |
19 |
Correct |
19 ms |
5284 KB |
Output is correct |
20 |
Correct |
20 ms |
5332 KB |
Output is correct |
21 |
Correct |
21 ms |
5308 KB |
Output is correct |
22 |
Correct |
40 ms |
10300 KB |
Output is correct |
23 |
Correct |
5 ms |
1588 KB |
Output is correct |
24 |
Correct |
15 ms |
4180 KB |
Output is correct |
25 |
Correct |
1 ms |
468 KB |
Output is correct |
26 |
Correct |
5 ms |
1336 KB |
Output is correct |
27 |
Correct |
3 ms |
1364 KB |
Output is correct |
28 |
Correct |
102 ms |
24184 KB |
Output is correct |
29 |
Correct |
128 ms |
32472 KB |
Output is correct |
30 |
Correct |
150 ms |
41044 KB |
Output is correct |
31 |
Correct |
154 ms |
41052 KB |
Output is correct |
32 |
Correct |
124 ms |
34000 KB |
Output is correct |
33 |
Correct |
159 ms |
41304 KB |
Output is correct |
34 |
Correct |
199 ms |
41300 KB |
Output is correct |
35 |
Correct |
87 ms |
16216 KB |
Output is correct |
36 |
Correct |
156 ms |
39440 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
40 ms |
24564 KB |
Output is correct |
2 |
Correct |
41 ms |
24528 KB |
Output is correct |
3 |
Correct |
70 ms |
27596 KB |
Output is correct |
4 |
Correct |
62 ms |
28192 KB |
Output is correct |
5 |
Correct |
125 ms |
34804 KB |
Output is correct |
6 |
Correct |
102 ms |
34088 KB |
Output is correct |
7 |
Correct |
98 ms |
34744 KB |
Output is correct |
8 |
Correct |
112 ms |
34748 KB |
Output is correct |
9 |
Correct |
102 ms |
37548 KB |
Output is correct |
10 |
Correct |
77 ms |
22172 KB |
Output is correct |
11 |
Correct |
196 ms |
44196 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
304 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
39 ms |
24524 KB |
Output is correct |
19 |
Correct |
60 ms |
24524 KB |
Output is correct |
20 |
Correct |
40 ms |
24520 KB |
Output is correct |
21 |
Correct |
40 ms |
24524 KB |
Output is correct |
22 |
Correct |
141 ms |
38912 KB |
Output is correct |
23 |
Correct |
206 ms |
50004 KB |
Output is correct |
24 |
Correct |
201 ms |
50976 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
74 ms |
28092 KB |
Output is correct |
2 |
Correct |
92 ms |
31524 KB |
Output is correct |
3 |
Correct |
42 ms |
24572 KB |
Output is correct |
4 |
Correct |
40 ms |
24504 KB |
Output is correct |
5 |
Correct |
171 ms |
46884 KB |
Output is correct |
6 |
Correct |
272 ms |
61280 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
107 ms |
33980 KB |
Output is correct |
9 |
Correct |
127 ms |
38736 KB |
Output is correct |
10 |
Correct |
71 ms |
28060 KB |
Output is correct |
11 |
Correct |
82 ms |
31532 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
40 ms |
24524 KB |
Output is correct |
17 |
Correct |
41 ms |
24516 KB |
Output is correct |
18 |
Correct |
77 ms |
29548 KB |
Output is correct |
19 |
Correct |
98 ms |
33284 KB |
Output is correct |
20 |
Correct |
70 ms |
29660 KB |
Output is correct |
21 |
Correct |
78 ms |
32848 KB |
Output is correct |
22 |
Correct |
90 ms |
29632 KB |
Output is correct |
23 |
Correct |
105 ms |
32776 KB |
Output is correct |
24 |
Correct |
40 ms |
24564 KB |
Output is correct |
25 |
Correct |
41 ms |
24528 KB |
Output is correct |
26 |
Correct |
70 ms |
27596 KB |
Output is correct |
27 |
Correct |
62 ms |
28192 KB |
Output is correct |
28 |
Correct |
125 ms |
34804 KB |
Output is correct |
29 |
Correct |
102 ms |
34088 KB |
Output is correct |
30 |
Correct |
98 ms |
34744 KB |
Output is correct |
31 |
Correct |
112 ms |
34748 KB |
Output is correct |
32 |
Correct |
0 ms |
212 KB |
Output is correct |
33 |
Correct |
0 ms |
212 KB |
Output is correct |
34 |
Correct |
0 ms |
212 KB |
Output is correct |
35 |
Correct |
1 ms |
212 KB |
Output is correct |
36 |
Correct |
1 ms |
212 KB |
Output is correct |
37 |
Correct |
1 ms |
340 KB |
Output is correct |
38 |
Correct |
1 ms |
212 KB |
Output is correct |
39 |
Correct |
0 ms |
212 KB |
Output is correct |
40 |
Correct |
1 ms |
340 KB |
Output is correct |
41 |
Correct |
2 ms |
576 KB |
Output is correct |
42 |
Correct |
1 ms |
340 KB |
Output is correct |
43 |
Correct |
2 ms |
468 KB |
Output is correct |
44 |
Correct |
1 ms |
304 KB |
Output is correct |
45 |
Correct |
1 ms |
440 KB |
Output is correct |
46 |
Correct |
1 ms |
340 KB |
Output is correct |
47 |
Correct |
2 ms |
468 KB |
Output is correct |
48 |
Correct |
19 ms |
5460 KB |
Output is correct |
49 |
Correct |
18 ms |
5556 KB |
Output is correct |
50 |
Correct |
19 ms |
5284 KB |
Output is correct |
51 |
Correct |
20 ms |
5332 KB |
Output is correct |
52 |
Correct |
21 ms |
5308 KB |
Output is correct |
53 |
Correct |
40 ms |
10300 KB |
Output is correct |
54 |
Correct |
5 ms |
1588 KB |
Output is correct |
55 |
Correct |
15 ms |
4180 KB |
Output is correct |
56 |
Correct |
1 ms |
468 KB |
Output is correct |
57 |
Correct |
5 ms |
1336 KB |
Output is correct |
58 |
Correct |
3 ms |
1364 KB |
Output is correct |
59 |
Correct |
102 ms |
24184 KB |
Output is correct |
60 |
Correct |
128 ms |
32472 KB |
Output is correct |
61 |
Correct |
150 ms |
41044 KB |
Output is correct |
62 |
Correct |
154 ms |
41052 KB |
Output is correct |
63 |
Correct |
124 ms |
34000 KB |
Output is correct |
64 |
Correct |
159 ms |
41304 KB |
Output is correct |
65 |
Correct |
199 ms |
41300 KB |
Output is correct |
66 |
Correct |
87 ms |
16216 KB |
Output is correct |
67 |
Correct |
156 ms |
39440 KB |
Output is correct |
68 |
Correct |
102 ms |
37548 KB |
Output is correct |
69 |
Correct |
77 ms |
22172 KB |
Output is correct |
70 |
Correct |
196 ms |
44196 KB |
Output is correct |
71 |
Correct |
1 ms |
212 KB |
Output is correct |
72 |
Correct |
1 ms |
304 KB |
Output is correct |
73 |
Correct |
1 ms |
212 KB |
Output is correct |
74 |
Correct |
1 ms |
212 KB |
Output is correct |
75 |
Correct |
1 ms |
212 KB |
Output is correct |
76 |
Correct |
1 ms |
212 KB |
Output is correct |
77 |
Correct |
39 ms |
24524 KB |
Output is correct |
78 |
Correct |
60 ms |
24524 KB |
Output is correct |
79 |
Correct |
40 ms |
24520 KB |
Output is correct |
80 |
Correct |
40 ms |
24524 KB |
Output is correct |
81 |
Correct |
141 ms |
38912 KB |
Output is correct |
82 |
Correct |
206 ms |
50004 KB |
Output is correct |
83 |
Correct |
201 ms |
50976 KB |
Output is correct |
84 |
Correct |
216 ms |
57080 KB |
Output is correct |
85 |
Correct |
228 ms |
58856 KB |
Output is correct |
86 |
Correct |
302 ms |
64568 KB |
Output is correct |
87 |
Correct |
307 ms |
67248 KB |
Output is correct |
88 |
Correct |
1 ms |
216 KB |
Output is correct |
89 |
Correct |
282 ms |
67296 KB |
Output is correct |
90 |
Correct |
278 ms |
62828 KB |
Output is correct |
91 |
Correct |
216 ms |
57536 KB |
Output is correct |