#include "fish.h"
#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 ll oo = 1e18;
void cmax(ll& a, ll b) {a=max(a,b);}
long long max_weights(int N, int M, std::vector<int> X, std::vector<int> Y, std::vector<int> W) {
vvi ys(N+1);
bool allEven=1;
ll total=0;
for(int i=0;i<M;++i) {
if(X[i]%2!=0) allEven=0;
Y[i]++,X[i]++;
total+=W[i];
ys[X[i]].push_back(i);
}
vector<vector<ll>> pref(N+1,{0});
{
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());
assert(pref[x].size()==v.size());
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]);
}
// falling to rising, case 2
// {
// 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[1][j]-getS(x,ys[x-1][j])-pref[x-1][j]);
// ++j;
// }
// cmax(ndp[0][i], best + pref[x][i] + getS(x-1, ys[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])));;
assert(ans<=total);
return ans;
}
Compilation message
fish.cpp: In function 'long long int max_weights(int, int, std::vector<int>, std::vector<int>, std::vector<int>)':
fish.cpp:17:10: warning: variable 'allEven' set but not used [-Wunused-but-set-variable]
17 | bool allEven=1;
| ^~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
58 ms |
14276 KB |
Output is correct |
2 |
Correct |
65 ms |
16248 KB |
Output is correct |
3 |
Correct |
30 ms |
11152 KB |
Output is correct |
4 |
Correct |
27 ms |
11220 KB |
Output is correct |
5 |
Correct |
146 ms |
23256 KB |
Output is correct |
6 |
Correct |
163 ms |
23408 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
98 ms |
18324 KB |
Output is correct |
3 |
Correct |
120 ms |
21444 KB |
Output is correct |
4 |
Correct |
59 ms |
14164 KB |
Output is correct |
5 |
Correct |
79 ms |
16304 KB |
Output is correct |
6 |
Correct |
0 ms |
212 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 |
212 KB |
Output is correct |
10 |
Correct |
27 ms |
11204 KB |
Output is correct |
11 |
Correct |
26 ms |
11184 KB |
Output is correct |
12 |
Correct |
56 ms |
14316 KB |
Output is correct |
13 |
Correct |
63 ms |
16324 KB |
Output is correct |
14 |
Correct |
57 ms |
14268 KB |
Output is correct |
15 |
Correct |
69 ms |
15864 KB |
Output is correct |
16 |
Correct |
59 ms |
14292 KB |
Output is correct |
17 |
Correct |
62 ms |
15800 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
30 ms |
11236 KB |
Output is correct |
2 |
Correct |
26 ms |
11228 KB |
Output is correct |
3 |
Correct |
50 ms |
12212 KB |
Output is correct |
4 |
Correct |
46 ms |
12628 KB |
Output is correct |
5 |
Correct |
78 ms |
15172 KB |
Output is correct |
6 |
Correct |
72 ms |
15196 KB |
Output is correct |
7 |
Correct |
78 ms |
15204 KB |
Output is correct |
8 |
Correct |
90 ms |
15208 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 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 |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 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 |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
19 ms |
2004 KB |
Output is correct |
18 |
Correct |
17 ms |
2388 KB |
Output is correct |
19 |
Correct |
19 ms |
2244 KB |
Output is correct |
20 |
Correct |
16 ms |
2248 KB |
Output is correct |
21 |
Correct |
16 ms |
2248 KB |
Output is correct |
22 |
Correct |
33 ms |
4300 KB |
Output is correct |
23 |
Correct |
4 ms |
596 KB |
Output is correct |
24 |
Correct |
16 ms |
1492 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
4 ms |
596 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 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 |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
19 ms |
2004 KB |
Output is correct |
18 |
Correct |
17 ms |
2388 KB |
Output is correct |
19 |
Correct |
19 ms |
2244 KB |
Output is correct |
20 |
Correct |
16 ms |
2248 KB |
Output is correct |
21 |
Correct |
16 ms |
2248 KB |
Output is correct |
22 |
Correct |
33 ms |
4300 KB |
Output is correct |
23 |
Correct |
4 ms |
596 KB |
Output is correct |
24 |
Correct |
16 ms |
1492 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
4 ms |
596 KB |
Output is correct |
27 |
Correct |
3 ms |
724 KB |
Output is correct |
28 |
Correct |
92 ms |
9864 KB |
Output is correct |
29 |
Correct |
132 ms |
12544 KB |
Output is correct |
30 |
Correct |
134 ms |
12332 KB |
Output is correct |
31 |
Correct |
142 ms |
12260 KB |
Output is correct |
32 |
Correct |
115 ms |
14164 KB |
Output is correct |
33 |
Correct |
139 ms |
12256 KB |
Output is correct |
34 |
Correct |
120 ms |
12264 KB |
Output is correct |
35 |
Correct |
46 ms |
5376 KB |
Output is correct |
36 |
Correct |
142 ms |
13516 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
30 ms |
11236 KB |
Output is correct |
2 |
Correct |
26 ms |
11228 KB |
Output is correct |
3 |
Correct |
50 ms |
12212 KB |
Output is correct |
4 |
Correct |
46 ms |
12628 KB |
Output is correct |
5 |
Correct |
78 ms |
15172 KB |
Output is correct |
6 |
Correct |
72 ms |
15196 KB |
Output is correct |
7 |
Correct |
78 ms |
15204 KB |
Output is correct |
8 |
Correct |
90 ms |
15208 KB |
Output is correct |
9 |
Correct |
76 ms |
15072 KB |
Output is correct |
10 |
Correct |
51 ms |
9292 KB |
Output is correct |
11 |
Correct |
114 ms |
18408 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
34 ms |
11312 KB |
Output is correct |
19 |
Correct |
34 ms |
11228 KB |
Output is correct |
20 |
Correct |
26 ms |
11144 KB |
Output is correct |
21 |
Correct |
26 ms |
11168 KB |
Output is correct |
22 |
Correct |
76 ms |
15340 KB |
Output is correct |
23 |
Correct |
113 ms |
18396 KB |
Output is correct |
24 |
Correct |
118 ms |
18480 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
58 ms |
14276 KB |
Output is correct |
2 |
Correct |
65 ms |
16248 KB |
Output is correct |
3 |
Correct |
30 ms |
11152 KB |
Output is correct |
4 |
Correct |
27 ms |
11220 KB |
Output is correct |
5 |
Correct |
146 ms |
23256 KB |
Output is correct |
6 |
Correct |
163 ms |
23408 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
98 ms |
18324 KB |
Output is correct |
9 |
Correct |
120 ms |
21444 KB |
Output is correct |
10 |
Correct |
59 ms |
14164 KB |
Output is correct |
11 |
Correct |
79 ms |
16304 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
27 ms |
11204 KB |
Output is correct |
17 |
Correct |
26 ms |
11184 KB |
Output is correct |
18 |
Correct |
56 ms |
14316 KB |
Output is correct |
19 |
Correct |
63 ms |
16324 KB |
Output is correct |
20 |
Correct |
57 ms |
14268 KB |
Output is correct |
21 |
Correct |
69 ms |
15864 KB |
Output is correct |
22 |
Correct |
59 ms |
14292 KB |
Output is correct |
23 |
Correct |
62 ms |
15800 KB |
Output is correct |
24 |
Correct |
30 ms |
11236 KB |
Output is correct |
25 |
Correct |
26 ms |
11228 KB |
Output is correct |
26 |
Correct |
50 ms |
12212 KB |
Output is correct |
27 |
Correct |
46 ms |
12628 KB |
Output is correct |
28 |
Correct |
78 ms |
15172 KB |
Output is correct |
29 |
Correct |
72 ms |
15196 KB |
Output is correct |
30 |
Correct |
78 ms |
15204 KB |
Output is correct |
31 |
Correct |
90 ms |
15208 KB |
Output is correct |
32 |
Correct |
0 ms |
212 KB |
Output is correct |
33 |
Correct |
0 ms |
212 KB |
Output is correct |
34 |
Correct |
1 ms |
212 KB |
Output is correct |
35 |
Correct |
1 ms |
212 KB |
Output is correct |
36 |
Correct |
0 ms |
212 KB |
Output is correct |
37 |
Correct |
0 ms |
212 KB |
Output is correct |
38 |
Correct |
0 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 |
1 ms |
340 KB |
Output is correct |
42 |
Correct |
1 ms |
340 KB |
Output is correct |
43 |
Correct |
1 ms |
340 KB |
Output is correct |
44 |
Correct |
0 ms |
212 KB |
Output is correct |
45 |
Correct |
1 ms |
340 KB |
Output is correct |
46 |
Correct |
1 ms |
340 KB |
Output is correct |
47 |
Correct |
1 ms |
340 KB |
Output is correct |
48 |
Correct |
19 ms |
2004 KB |
Output is correct |
49 |
Correct |
17 ms |
2388 KB |
Output is correct |
50 |
Correct |
19 ms |
2244 KB |
Output is correct |
51 |
Correct |
16 ms |
2248 KB |
Output is correct |
52 |
Correct |
16 ms |
2248 KB |
Output is correct |
53 |
Correct |
33 ms |
4300 KB |
Output is correct |
54 |
Correct |
4 ms |
596 KB |
Output is correct |
55 |
Correct |
16 ms |
1492 KB |
Output is correct |
56 |
Correct |
1 ms |
340 KB |
Output is correct |
57 |
Correct |
4 ms |
596 KB |
Output is correct |
58 |
Correct |
3 ms |
724 KB |
Output is correct |
59 |
Correct |
92 ms |
9864 KB |
Output is correct |
60 |
Correct |
132 ms |
12544 KB |
Output is correct |
61 |
Correct |
134 ms |
12332 KB |
Output is correct |
62 |
Correct |
142 ms |
12260 KB |
Output is correct |
63 |
Correct |
115 ms |
14164 KB |
Output is correct |
64 |
Correct |
139 ms |
12256 KB |
Output is correct |
65 |
Correct |
120 ms |
12264 KB |
Output is correct |
66 |
Correct |
46 ms |
5376 KB |
Output is correct |
67 |
Correct |
142 ms |
13516 KB |
Output is correct |
68 |
Correct |
76 ms |
15072 KB |
Output is correct |
69 |
Correct |
51 ms |
9292 KB |
Output is correct |
70 |
Correct |
114 ms |
18408 KB |
Output is correct |
71 |
Correct |
1 ms |
212 KB |
Output is correct |
72 |
Correct |
0 ms |
212 KB |
Output is correct |
73 |
Correct |
0 ms |
212 KB |
Output is correct |
74 |
Correct |
0 ms |
212 KB |
Output is correct |
75 |
Correct |
0 ms |
212 KB |
Output is correct |
76 |
Correct |
0 ms |
212 KB |
Output is correct |
77 |
Correct |
34 ms |
11312 KB |
Output is correct |
78 |
Correct |
34 ms |
11228 KB |
Output is correct |
79 |
Correct |
26 ms |
11144 KB |
Output is correct |
80 |
Correct |
26 ms |
11168 KB |
Output is correct |
81 |
Correct |
76 ms |
15340 KB |
Output is correct |
82 |
Correct |
113 ms |
18396 KB |
Output is correct |
83 |
Correct |
118 ms |
18480 KB |
Output is correct |
84 |
Correct |
181 ms |
22328 KB |
Output is correct |
85 |
Correct |
176 ms |
23424 KB |
Output is correct |
86 |
Correct |
178 ms |
22940 KB |
Output is correct |
87 |
Correct |
192 ms |
24276 KB |
Output is correct |
88 |
Correct |
0 ms |
212 KB |
Output is correct |
89 |
Correct |
220 ms |
24152 KB |
Output is correct |
90 |
Correct |
172 ms |
23460 KB |
Output is correct |
91 |
Correct |
158 ms |
23648 KB |
Output is correct |