#include "fish.h"
#include <bits/stdc++.h>
using namespace std;
typedef long long int64;
const int maxn = 2e5 + 2;
map <int64, int64> dp [maxn][2];
int64 mx [maxn];
vector <pair <int64, int64> > p[maxn];
vector <pair <int64, int64> > pref [maxn], suff[maxn];
vector <int64> pos [maxn];
int64 psum (int64 i, int64 j) {
auto it = lower_bound(p[i].begin(), p[i].end(), make_pair(j, (int64) 1e18));
if (it == p[i].begin()) return 0;
return (--it)->second;
}
int64 max_weights(int n, int m, vector<int> x, vector<int> y,
vector<int> w) {
for (int i = 0; i < m; i++) {
pos[++x[i]].push_back(++y[i]);
p[x[i]].push_back({y[i], w[i]});
}
for (int i = 1; i <= n; i++){
sort(p[i].begin(), p[i].end());
sort(pos[i].begin(), pos[i].end());
int64 sum = 0;
for (auto& [j, c]: p[i]) {
int64 t = sum;
sum+=c;
c+=t;
}
}
for (int i = 1; i <= n; i++){
vector <int64> ps = {0, n};
for (auto j: pos[i-1]) ps.push_back(j);
for (auto j: pos[i+1]) ps.push_back(j);
sort(ps.begin(), ps.end());
for (auto j: ps){
auto it = lower_bound(suff[i-1].begin(), suff[i-1].end(), make_pair(j, -1ll));
dp[i][j][0] = -psum(i, j);
if (it != suff[i-1].end()) dp[i][j][0] += it->second;
it = lower_bound(pref[i - 1].begin(), pref[i - 1].end(), make_pair(j, (int64) 1e18));
dp[i][j][1] = ((i - 2 >= 0) ? mx[i-2]: 0) + psum(i-1, j);
if (it != pref[i-1].begin()) dp[i][j][1] = max(dp[i][j][1], (--it)->second + psum(i-1, j));
mx[i] = max({mx[i], dp[i][j][0], dp[i][j][1]});
}
for (auto j: ps) {
pref[i].push_back(make_pair(j, max((pref[i].empty() ? 0ll: pref[i].back().second), dp[i][j][1] - psum(i, j))));
}
reverse(ps.begin(), ps.end());
for (auto j: ps) {
suff[i].push_back(make_pair(j, max((suff[i].empty() ? 0ll: suff[i].back().second), max(dp[i][j][0], dp[i][j][1]) + psum(i + 1, j))));
}
reverse(suff[i].begin(), suff[i].end());
}
return mx[n];
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
123 ms |
75300 KB |
Output is correct |
2 |
Correct |
148 ms |
80940 KB |
Output is correct |
3 |
Correct |
82 ms |
66792 KB |
Output is correct |
4 |
Correct |
91 ms |
66860 KB |
Output is correct |
5 |
Correct |
344 ms |
115352 KB |
Output is correct |
6 |
Correct |
500 ms |
126676 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
37844 KB |
Output is correct |
2 |
Correct |
218 ms |
84808 KB |
Output is correct |
3 |
Correct |
252 ms |
92016 KB |
Output is correct |
4 |
Correct |
125 ms |
75284 KB |
Output is correct |
5 |
Correct |
148 ms |
80884 KB |
Output is correct |
6 |
Correct |
18 ms |
37872 KB |
Output is correct |
7 |
Correct |
18 ms |
37820 KB |
Output is correct |
8 |
Correct |
17 ms |
37844 KB |
Output is correct |
9 |
Correct |
16 ms |
37832 KB |
Output is correct |
10 |
Correct |
78 ms |
66728 KB |
Output is correct |
11 |
Correct |
100 ms |
66800 KB |
Output is correct |
12 |
Correct |
165 ms |
78384 KB |
Output is correct |
13 |
Correct |
157 ms |
84048 KB |
Output is correct |
14 |
Correct |
146 ms |
76844 KB |
Output is correct |
15 |
Correct |
161 ms |
78452 KB |
Output is correct |
16 |
Correct |
133 ms |
76816 KB |
Output is correct |
17 |
Correct |
151 ms |
81120 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
79 ms |
66764 KB |
Output is correct |
2 |
Correct |
81 ms |
66856 KB |
Output is correct |
3 |
Correct |
143 ms |
79408 KB |
Output is correct |
4 |
Correct |
127 ms |
77552 KB |
Output is correct |
5 |
Correct |
202 ms |
94224 KB |
Output is correct |
6 |
Correct |
197 ms |
94280 KB |
Output is correct |
7 |
Correct |
195 ms |
94340 KB |
Output is correct |
8 |
Correct |
198 ms |
94116 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
18 ms |
37844 KB |
Output is correct |
2 |
Correct |
17 ms |
37844 KB |
Output is correct |
3 |
Correct |
18 ms |
37844 KB |
Output is correct |
4 |
Correct |
18 ms |
37768 KB |
Output is correct |
5 |
Correct |
18 ms |
37844 KB |
Output is correct |
6 |
Correct |
18 ms |
37812 KB |
Output is correct |
7 |
Correct |
18 ms |
37864 KB |
Output is correct |
8 |
Correct |
18 ms |
37844 KB |
Output is correct |
9 |
Correct |
21 ms |
37972 KB |
Output is correct |
10 |
Correct |
20 ms |
38340 KB |
Output is correct |
11 |
Correct |
19 ms |
37972 KB |
Output is correct |
12 |
Correct |
20 ms |
38168 KB |
Output is correct |
13 |
Correct |
21 ms |
37840 KB |
Output is correct |
14 |
Correct |
19 ms |
38108 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
18 ms |
37844 KB |
Output is correct |
2 |
Correct |
17 ms |
37844 KB |
Output is correct |
3 |
Correct |
18 ms |
37844 KB |
Output is correct |
4 |
Correct |
18 ms |
37768 KB |
Output is correct |
5 |
Correct |
18 ms |
37844 KB |
Output is correct |
6 |
Correct |
18 ms |
37812 KB |
Output is correct |
7 |
Correct |
18 ms |
37864 KB |
Output is correct |
8 |
Correct |
18 ms |
37844 KB |
Output is correct |
9 |
Correct |
21 ms |
37972 KB |
Output is correct |
10 |
Correct |
20 ms |
38340 KB |
Output is correct |
11 |
Correct |
19 ms |
37972 KB |
Output is correct |
12 |
Correct |
20 ms |
38168 KB |
Output is correct |
13 |
Correct |
21 ms |
37840 KB |
Output is correct |
14 |
Correct |
19 ms |
38108 KB |
Output is correct |
15 |
Correct |
18 ms |
38000 KB |
Output is correct |
16 |
Correct |
20 ms |
38100 KB |
Output is correct |
17 |
Correct |
64 ms |
43288 KB |
Output is correct |
18 |
Correct |
65 ms |
45200 KB |
Output is correct |
19 |
Correct |
59 ms |
45692 KB |
Output is correct |
20 |
Correct |
59 ms |
45716 KB |
Output is correct |
21 |
Correct |
59 ms |
45772 KB |
Output is correct |
22 |
Correct |
108 ms |
51704 KB |
Output is correct |
23 |
Correct |
27 ms |
39252 KB |
Output is correct |
24 |
Correct |
48 ms |
41952 KB |
Output is correct |
25 |
Correct |
19 ms |
38100 KB |
Output is correct |
26 |
Correct |
26 ms |
39092 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
18 ms |
37844 KB |
Output is correct |
2 |
Correct |
17 ms |
37844 KB |
Output is correct |
3 |
Correct |
18 ms |
37844 KB |
Output is correct |
4 |
Correct |
18 ms |
37768 KB |
Output is correct |
5 |
Correct |
18 ms |
37844 KB |
Output is correct |
6 |
Correct |
18 ms |
37812 KB |
Output is correct |
7 |
Correct |
18 ms |
37864 KB |
Output is correct |
8 |
Correct |
18 ms |
37844 KB |
Output is correct |
9 |
Correct |
21 ms |
37972 KB |
Output is correct |
10 |
Correct |
20 ms |
38340 KB |
Output is correct |
11 |
Correct |
19 ms |
37972 KB |
Output is correct |
12 |
Correct |
20 ms |
38168 KB |
Output is correct |
13 |
Correct |
21 ms |
37840 KB |
Output is correct |
14 |
Correct |
19 ms |
38108 KB |
Output is correct |
15 |
Correct |
18 ms |
38000 KB |
Output is correct |
16 |
Correct |
20 ms |
38100 KB |
Output is correct |
17 |
Correct |
64 ms |
43288 KB |
Output is correct |
18 |
Correct |
65 ms |
45200 KB |
Output is correct |
19 |
Correct |
59 ms |
45692 KB |
Output is correct |
20 |
Correct |
59 ms |
45716 KB |
Output is correct |
21 |
Correct |
59 ms |
45772 KB |
Output is correct |
22 |
Correct |
108 ms |
51704 KB |
Output is correct |
23 |
Correct |
27 ms |
39252 KB |
Output is correct |
24 |
Correct |
48 ms |
41952 KB |
Output is correct |
25 |
Correct |
19 ms |
38100 KB |
Output is correct |
26 |
Correct |
26 ms |
39092 KB |
Output is correct |
27 |
Correct |
22 ms |
39448 KB |
Output is correct |
28 |
Correct |
257 ms |
67848 KB |
Output is correct |
29 |
Correct |
358 ms |
76212 KB |
Output is correct |
30 |
Correct |
310 ms |
79428 KB |
Output is correct |
31 |
Correct |
323 ms |
79372 KB |
Output is correct |
32 |
Correct |
339 ms |
80576 KB |
Output is correct |
33 |
Correct |
320 ms |
79280 KB |
Output is correct |
34 |
Correct |
318 ms |
79308 KB |
Output is correct |
35 |
Correct |
135 ms |
57056 KB |
Output is correct |
36 |
Correct |
337 ms |
82356 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
79 ms |
66764 KB |
Output is correct |
2 |
Correct |
81 ms |
66856 KB |
Output is correct |
3 |
Correct |
143 ms |
79408 KB |
Output is correct |
4 |
Correct |
127 ms |
77552 KB |
Output is correct |
5 |
Correct |
202 ms |
94224 KB |
Output is correct |
6 |
Correct |
197 ms |
94280 KB |
Output is correct |
7 |
Correct |
195 ms |
94340 KB |
Output is correct |
8 |
Correct |
198 ms |
94116 KB |
Output is correct |
9 |
Correct |
216 ms |
94212 KB |
Output is correct |
10 |
Correct |
149 ms |
74652 KB |
Output is correct |
11 |
Correct |
288 ms |
111528 KB |
Output is correct |
12 |
Correct |
18 ms |
37820 KB |
Output is correct |
13 |
Correct |
18 ms |
37776 KB |
Output is correct |
14 |
Correct |
17 ms |
37844 KB |
Output is correct |
15 |
Correct |
19 ms |
37876 KB |
Output is correct |
16 |
Correct |
18 ms |
37812 KB |
Output is correct |
17 |
Correct |
18 ms |
37796 KB |
Output is correct |
18 |
Correct |
80 ms |
66756 KB |
Output is correct |
19 |
Correct |
86 ms |
66740 KB |
Output is correct |
20 |
Correct |
79 ms |
66760 KB |
Output is correct |
21 |
Correct |
81 ms |
66812 KB |
Output is correct |
22 |
Correct |
253 ms |
92172 KB |
Output is correct |
23 |
Correct |
312 ms |
110876 KB |
Output is correct |
24 |
Correct |
309 ms |
111000 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
123 ms |
75300 KB |
Output is correct |
2 |
Correct |
148 ms |
80940 KB |
Output is correct |
3 |
Correct |
82 ms |
66792 KB |
Output is correct |
4 |
Correct |
91 ms |
66860 KB |
Output is correct |
5 |
Correct |
344 ms |
115352 KB |
Output is correct |
6 |
Correct |
500 ms |
126676 KB |
Output is correct |
7 |
Correct |
20 ms |
37844 KB |
Output is correct |
8 |
Correct |
218 ms |
84808 KB |
Output is correct |
9 |
Correct |
252 ms |
92016 KB |
Output is correct |
10 |
Correct |
125 ms |
75284 KB |
Output is correct |
11 |
Correct |
148 ms |
80884 KB |
Output is correct |
12 |
Correct |
18 ms |
37872 KB |
Output is correct |
13 |
Correct |
18 ms |
37820 KB |
Output is correct |
14 |
Correct |
17 ms |
37844 KB |
Output is correct |
15 |
Correct |
16 ms |
37832 KB |
Output is correct |
16 |
Correct |
78 ms |
66728 KB |
Output is correct |
17 |
Correct |
100 ms |
66800 KB |
Output is correct |
18 |
Correct |
165 ms |
78384 KB |
Output is correct |
19 |
Correct |
157 ms |
84048 KB |
Output is correct |
20 |
Correct |
146 ms |
76844 KB |
Output is correct |
21 |
Correct |
161 ms |
78452 KB |
Output is correct |
22 |
Correct |
133 ms |
76816 KB |
Output is correct |
23 |
Correct |
151 ms |
81120 KB |
Output is correct |
24 |
Correct |
79 ms |
66764 KB |
Output is correct |
25 |
Correct |
81 ms |
66856 KB |
Output is correct |
26 |
Correct |
143 ms |
79408 KB |
Output is correct |
27 |
Correct |
127 ms |
77552 KB |
Output is correct |
28 |
Correct |
202 ms |
94224 KB |
Output is correct |
29 |
Correct |
197 ms |
94280 KB |
Output is correct |
30 |
Correct |
195 ms |
94340 KB |
Output is correct |
31 |
Correct |
198 ms |
94116 KB |
Output is correct |
32 |
Correct |
18 ms |
37844 KB |
Output is correct |
33 |
Correct |
17 ms |
37844 KB |
Output is correct |
34 |
Correct |
18 ms |
37844 KB |
Output is correct |
35 |
Correct |
18 ms |
37768 KB |
Output is correct |
36 |
Correct |
18 ms |
37844 KB |
Output is correct |
37 |
Correct |
18 ms |
37812 KB |
Output is correct |
38 |
Correct |
18 ms |
37864 KB |
Output is correct |
39 |
Correct |
18 ms |
37844 KB |
Output is correct |
40 |
Correct |
21 ms |
37972 KB |
Output is correct |
41 |
Correct |
20 ms |
38340 KB |
Output is correct |
42 |
Correct |
19 ms |
37972 KB |
Output is correct |
43 |
Correct |
20 ms |
38168 KB |
Output is correct |
44 |
Correct |
21 ms |
37840 KB |
Output is correct |
45 |
Correct |
19 ms |
38108 KB |
Output is correct |
46 |
Correct |
18 ms |
38000 KB |
Output is correct |
47 |
Correct |
20 ms |
38100 KB |
Output is correct |
48 |
Correct |
64 ms |
43288 KB |
Output is correct |
49 |
Correct |
65 ms |
45200 KB |
Output is correct |
50 |
Correct |
59 ms |
45692 KB |
Output is correct |
51 |
Correct |
59 ms |
45716 KB |
Output is correct |
52 |
Correct |
59 ms |
45772 KB |
Output is correct |
53 |
Correct |
108 ms |
51704 KB |
Output is correct |
54 |
Correct |
27 ms |
39252 KB |
Output is correct |
55 |
Correct |
48 ms |
41952 KB |
Output is correct |
56 |
Correct |
19 ms |
38100 KB |
Output is correct |
57 |
Correct |
26 ms |
39092 KB |
Output is correct |
58 |
Correct |
22 ms |
39448 KB |
Output is correct |
59 |
Correct |
257 ms |
67848 KB |
Output is correct |
60 |
Correct |
358 ms |
76212 KB |
Output is correct |
61 |
Correct |
310 ms |
79428 KB |
Output is correct |
62 |
Correct |
323 ms |
79372 KB |
Output is correct |
63 |
Correct |
339 ms |
80576 KB |
Output is correct |
64 |
Correct |
320 ms |
79280 KB |
Output is correct |
65 |
Correct |
318 ms |
79308 KB |
Output is correct |
66 |
Correct |
135 ms |
57056 KB |
Output is correct |
67 |
Correct |
337 ms |
82356 KB |
Output is correct |
68 |
Correct |
216 ms |
94212 KB |
Output is correct |
69 |
Correct |
149 ms |
74652 KB |
Output is correct |
70 |
Correct |
288 ms |
111528 KB |
Output is correct |
71 |
Correct |
18 ms |
37820 KB |
Output is correct |
72 |
Correct |
18 ms |
37776 KB |
Output is correct |
73 |
Correct |
17 ms |
37844 KB |
Output is correct |
74 |
Correct |
19 ms |
37876 KB |
Output is correct |
75 |
Correct |
18 ms |
37812 KB |
Output is correct |
76 |
Correct |
18 ms |
37796 KB |
Output is correct |
77 |
Correct |
80 ms |
66756 KB |
Output is correct |
78 |
Correct |
86 ms |
66740 KB |
Output is correct |
79 |
Correct |
79 ms |
66760 KB |
Output is correct |
80 |
Correct |
81 ms |
66812 KB |
Output is correct |
81 |
Correct |
253 ms |
92172 KB |
Output is correct |
82 |
Correct |
312 ms |
110876 KB |
Output is correct |
83 |
Correct |
309 ms |
111000 KB |
Output is correct |
84 |
Correct |
486 ms |
104828 KB |
Output is correct |
85 |
Correct |
519 ms |
106896 KB |
Output is correct |
86 |
Correct |
519 ms |
124512 KB |
Output is correct |
87 |
Correct |
534 ms |
129344 KB |
Output is correct |
88 |
Correct |
19 ms |
37892 KB |
Output is correct |
89 |
Correct |
547 ms |
128804 KB |
Output is correct |
90 |
Correct |
416 ms |
118024 KB |
Output is correct |
91 |
Correct |
396 ms |
115396 KB |
Output is correct |