#include "fish.h"
#include "bits/stdc++.h"
using namespace std;
#define all(x) begin(x),end(x)
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> pi;
const ll oo = 1e18;
void cmax(ll& a, ll b) {a=max(a,b);}
ll max_weights(int N, int M, vector<int> X, vector<int> Y, vector<int> W) {
vvi ys(N+1);
ll total=0;
for(int i=0;i<M;++i) {
Y[i]++,X[i]++;
total+=W[i];
ys[X[i]].push_back(i);
}
vector<vector<ll>> pref(N+1,{0});
{ // preprocessing and coordinate compression.
int x=0;
for(auto& v : ys) {
sort(all(v),[&](int i, int j) {return Y[i]<Y[j];});
for(auto& id : v) {
pref[x].push_back(W[id]);
id = Y[id];
}
pref[x].push_back(0);
v.insert(v.begin(),0);
v.push_back(N+1);
partial_sum(all(pref[x]),pref[x].begin());
x++;
}
}
// dynamic programming
// DP[column][y-pos][state]
// state {0: rising, 1: falling}
// transition going up:
array<vector<ll>,2> dp = {vector<ll>{0LL,-oo},{-oo,-oo}};
ll top = -oo;
auto getS = [&](int x, int y) {
auto i = max(0LL,upper_bound(all(ys[x]),y)-ys[x].begin()-1LL);
return pref[x][i];
};
for(int x=1;x<=N;++x) {
int k = ys[x].size();
array<vector<ll>,2> ndp = {vector<ll>(k,-oo),vector<ll>(k,-oo)};
{ // falling to falling
int j = ys[x-1].size()-1;
ll best = -oo;
for(int i=k-1;i>=0;--i) {
while(j>=0 and ys[x-1][j]>=ys[x][i]) {
cmax(best,dp[1][j]);
--j;
}
cmax(ndp[1][i], best-getS(x-1,ys[x][i]) + pref[x][i]);
}
}
{ // rising to rising
int j = 0;
ll best = -oo;
for(int i=0;i<k;++i) {
while(j<int(ys[x-1].size()) and ys[x-1][j]<=ys[x][i]) {
cmax(best,dp[0][j]-getS(x,ys[x-1][j]));
++j;
}
cmax(ndp[0][i], best + pref[x][i]);
}
}
{ // falling to rising, far apart
ll best = *max_element(all(dp[1]));
for(int i=0;i<k;++i)
cmax(ndp[0][i],best+pref[x][i]);
}
// top to falling
for(int i=0;i<k;++i) {
cmax(ndp[1][i],pref[x][i]+top);
}
// rising to top
cmax(top,*max_element(all(dp[0])));
cmax(top,*max_element(all(dp[1])));
swap(dp,ndp);
}
// falling or top
auto ans = max(top,*max_element(all(dp[1])));;
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
46 ms |
15600 KB |
Output is correct |
2 |
Correct |
54 ms |
18088 KB |
Output is correct |
3 |
Correct |
22 ms |
11356 KB |
Output is correct |
4 |
Correct |
22 ms |
11192 KB |
Output is correct |
5 |
Correct |
136 ms |
29804 KB |
Output is correct |
6 |
Correct |
127 ms |
30152 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
94 ms |
21276 KB |
Output is correct |
3 |
Correct |
105 ms |
25052 KB |
Output is correct |
4 |
Correct |
48 ms |
15696 KB |
Output is correct |
5 |
Correct |
54 ms |
17996 KB |
Output is correct |
6 |
Correct |
0 ms |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
21 ms |
11356 KB |
Output is correct |
11 |
Correct |
23 ms |
11408 KB |
Output is correct |
12 |
Correct |
50 ms |
15604 KB |
Output is correct |
13 |
Correct |
61 ms |
17996 KB |
Output is correct |
14 |
Correct |
49 ms |
15820 KB |
Output is correct |
15 |
Correct |
55 ms |
17484 KB |
Output is correct |
16 |
Correct |
46 ms |
15704 KB |
Output is correct |
17 |
Correct |
51 ms |
17476 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
22 ms |
11352 KB |
Output is correct |
2 |
Correct |
21 ms |
11352 KB |
Output is correct |
3 |
Correct |
39 ms |
13140 KB |
Output is correct |
4 |
Correct |
36 ms |
13392 KB |
Output is correct |
5 |
Correct |
60 ms |
16712 KB |
Output is correct |
6 |
Correct |
56 ms |
16136 KB |
Output is correct |
7 |
Correct |
59 ms |
16724 KB |
Output is correct |
8 |
Correct |
61 ms |
16684 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
444 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
444 KB |
Output is correct |
15 |
Correct |
1 ms |
344 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
17 ms |
2952 KB |
Output is correct |
18 |
Correct |
16 ms |
3164 KB |
Output is correct |
19 |
Correct |
14 ms |
3128 KB |
Output is correct |
20 |
Correct |
14 ms |
3164 KB |
Output is correct |
21 |
Correct |
14 ms |
3164 KB |
Output is correct |
22 |
Correct |
28 ms |
5972 KB |
Output is correct |
23 |
Correct |
4 ms |
860 KB |
Output is correct |
24 |
Correct |
11 ms |
2172 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
3 ms |
820 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
444 KB |
Output is correct |
15 |
Correct |
1 ms |
344 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
17 ms |
2952 KB |
Output is correct |
18 |
Correct |
16 ms |
3164 KB |
Output is correct |
19 |
Correct |
14 ms |
3128 KB |
Output is correct |
20 |
Correct |
14 ms |
3164 KB |
Output is correct |
21 |
Correct |
14 ms |
3164 KB |
Output is correct |
22 |
Correct |
28 ms |
5972 KB |
Output is correct |
23 |
Correct |
4 ms |
860 KB |
Output is correct |
24 |
Correct |
11 ms |
2172 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
3 ms |
820 KB |
Output is correct |
27 |
Correct |
2 ms |
860 KB |
Output is correct |
28 |
Correct |
80 ms |
13832 KB |
Output is correct |
29 |
Correct |
122 ms |
18140 KB |
Output is correct |
30 |
Correct |
118 ms |
18140 KB |
Output is correct |
31 |
Correct |
110 ms |
18004 KB |
Output is correct |
32 |
Correct |
102 ms |
19280 KB |
Output is correct |
33 |
Correct |
108 ms |
18000 KB |
Output is correct |
34 |
Correct |
110 ms |
17900 KB |
Output is correct |
35 |
Correct |
40 ms |
7560 KB |
Output is correct |
36 |
Correct |
116 ms |
19176 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
22 ms |
11352 KB |
Output is correct |
2 |
Correct |
21 ms |
11352 KB |
Output is correct |
3 |
Correct |
39 ms |
13140 KB |
Output is correct |
4 |
Correct |
36 ms |
13392 KB |
Output is correct |
5 |
Correct |
60 ms |
16712 KB |
Output is correct |
6 |
Correct |
56 ms |
16136 KB |
Output is correct |
7 |
Correct |
59 ms |
16724 KB |
Output is correct |
8 |
Correct |
61 ms |
16684 KB |
Output is correct |
9 |
Correct |
58 ms |
16464 KB |
Output is correct |
10 |
Correct |
43 ms |
11088 KB |
Output is correct |
11 |
Correct |
90 ms |
21844 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
436 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
21 ms |
11408 KB |
Output is correct |
19 |
Correct |
21 ms |
11356 KB |
Output is correct |
20 |
Correct |
21 ms |
11356 KB |
Output is correct |
21 |
Correct |
21 ms |
11356 KB |
Output is correct |
22 |
Correct |
60 ms |
17488 KB |
Output is correct |
23 |
Correct |
94 ms |
22100 KB |
Output is correct |
24 |
Correct |
104 ms |
22488 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
46 ms |
15600 KB |
Output is correct |
2 |
Correct |
54 ms |
18088 KB |
Output is correct |
3 |
Correct |
22 ms |
11356 KB |
Output is correct |
4 |
Correct |
22 ms |
11192 KB |
Output is correct |
5 |
Correct |
136 ms |
29804 KB |
Output is correct |
6 |
Correct |
127 ms |
30152 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
94 ms |
21276 KB |
Output is correct |
9 |
Correct |
105 ms |
25052 KB |
Output is correct |
10 |
Correct |
48 ms |
15696 KB |
Output is correct |
11 |
Correct |
54 ms |
17996 KB |
Output is correct |
12 |
Correct |
0 ms |
344 KB |
Output is correct |
13 |
Correct |
0 ms |
344 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
21 ms |
11356 KB |
Output is correct |
17 |
Correct |
23 ms |
11408 KB |
Output is correct |
18 |
Correct |
50 ms |
15604 KB |
Output is correct |
19 |
Correct |
61 ms |
17996 KB |
Output is correct |
20 |
Correct |
49 ms |
15820 KB |
Output is correct |
21 |
Correct |
55 ms |
17484 KB |
Output is correct |
22 |
Correct |
46 ms |
15704 KB |
Output is correct |
23 |
Correct |
51 ms |
17476 KB |
Output is correct |
24 |
Correct |
22 ms |
11352 KB |
Output is correct |
25 |
Correct |
21 ms |
11352 KB |
Output is correct |
26 |
Correct |
39 ms |
13140 KB |
Output is correct |
27 |
Correct |
36 ms |
13392 KB |
Output is correct |
28 |
Correct |
60 ms |
16712 KB |
Output is correct |
29 |
Correct |
56 ms |
16136 KB |
Output is correct |
30 |
Correct |
59 ms |
16724 KB |
Output is correct |
31 |
Correct |
61 ms |
16684 KB |
Output is correct |
32 |
Correct |
0 ms |
348 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
0 ms |
348 KB |
Output is correct |
35 |
Correct |
0 ms |
348 KB |
Output is correct |
36 |
Correct |
0 ms |
348 KB |
Output is correct |
37 |
Correct |
0 ms |
348 KB |
Output is correct |
38 |
Correct |
0 ms |
348 KB |
Output is correct |
39 |
Correct |
0 ms |
348 KB |
Output is correct |
40 |
Correct |
1 ms |
348 KB |
Output is correct |
41 |
Correct |
1 ms |
604 KB |
Output is correct |
42 |
Correct |
1 ms |
348 KB |
Output is correct |
43 |
Correct |
1 ms |
348 KB |
Output is correct |
44 |
Correct |
0 ms |
348 KB |
Output is correct |
45 |
Correct |
1 ms |
444 KB |
Output is correct |
46 |
Correct |
1 ms |
344 KB |
Output is correct |
47 |
Correct |
1 ms |
348 KB |
Output is correct |
48 |
Correct |
17 ms |
2952 KB |
Output is correct |
49 |
Correct |
16 ms |
3164 KB |
Output is correct |
50 |
Correct |
14 ms |
3128 KB |
Output is correct |
51 |
Correct |
14 ms |
3164 KB |
Output is correct |
52 |
Correct |
14 ms |
3164 KB |
Output is correct |
53 |
Correct |
28 ms |
5972 KB |
Output is correct |
54 |
Correct |
4 ms |
860 KB |
Output is correct |
55 |
Correct |
11 ms |
2172 KB |
Output is correct |
56 |
Correct |
1 ms |
348 KB |
Output is correct |
57 |
Correct |
3 ms |
820 KB |
Output is correct |
58 |
Correct |
2 ms |
860 KB |
Output is correct |
59 |
Correct |
80 ms |
13832 KB |
Output is correct |
60 |
Correct |
122 ms |
18140 KB |
Output is correct |
61 |
Correct |
118 ms |
18140 KB |
Output is correct |
62 |
Correct |
110 ms |
18004 KB |
Output is correct |
63 |
Correct |
102 ms |
19280 KB |
Output is correct |
64 |
Correct |
108 ms |
18000 KB |
Output is correct |
65 |
Correct |
110 ms |
17900 KB |
Output is correct |
66 |
Correct |
40 ms |
7560 KB |
Output is correct |
67 |
Correct |
116 ms |
19176 KB |
Output is correct |
68 |
Correct |
58 ms |
16464 KB |
Output is correct |
69 |
Correct |
43 ms |
11088 KB |
Output is correct |
70 |
Correct |
90 ms |
21844 KB |
Output is correct |
71 |
Correct |
1 ms |
348 KB |
Output is correct |
72 |
Correct |
0 ms |
348 KB |
Output is correct |
73 |
Correct |
0 ms |
348 KB |
Output is correct |
74 |
Correct |
0 ms |
348 KB |
Output is correct |
75 |
Correct |
1 ms |
436 KB |
Output is correct |
76 |
Correct |
0 ms |
348 KB |
Output is correct |
77 |
Correct |
21 ms |
11408 KB |
Output is correct |
78 |
Correct |
21 ms |
11356 KB |
Output is correct |
79 |
Correct |
21 ms |
11356 KB |
Output is correct |
80 |
Correct |
21 ms |
11356 KB |
Output is correct |
81 |
Correct |
60 ms |
17488 KB |
Output is correct |
82 |
Correct |
94 ms |
22100 KB |
Output is correct |
83 |
Correct |
104 ms |
22488 KB |
Output is correct |
84 |
Correct |
141 ms |
27932 KB |
Output is correct |
85 |
Correct |
140 ms |
28456 KB |
Output is correct |
86 |
Correct |
142 ms |
29032 KB |
Output is correct |
87 |
Correct |
137 ms |
30292 KB |
Output is correct |
88 |
Correct |
0 ms |
348 KB |
Output is correct |
89 |
Correct |
138 ms |
30432 KB |
Output is correct |
90 |
Correct |
131 ms |
28496 KB |
Output is correct |
91 |
Correct |
122 ms |
28896 KB |
Output is correct |