#include "fish.h"
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
const int N = 100005;
vector<pair<int, int>> Y[N];
int c[N];
vector<ll> D[N], F[N];
ll E[N];
ll max_weights(int n, int m, vector<int> x, vector<int> y,
vector<int> w) {
for (int i = 0; i < m; i++)
Y[x[i]].emplace_back(y[i], w[i]);
for (int i = 0; i < n; i++)
sort(Y[i].begin(), Y[i].end()), c[i] = Y[i].size();
// initialize column 0:
E[0] = 0;
F[0].resize(c[0], -(ll)1e18);
D[0].resize(c[0]);
if (c[0]) {
D[0][0] = Y[0][0].second;
for (int j = 1; j < c[0]; j++) {
D[0][j] = D[0][j - 1] + Y[0][j].second;
}
}
// begin dp:
for (int i = 1; i < n; i++) {
//cout << "begin " << i << '\n';
E[i] = E[i - 1];
for (int j = 0; j < c[i - 1]; j++)
E[i] = max(max(E[i], D[i - 1][j]), F[i - 1][j]);
//cout << "E[i] " << E[i] << '\n';
if (!c[i]) continue;
D[i].resize(c[i]);
F[i].resize(c[i]);
vector<ll> below(c[i]), above(c[i]);
// compute below
int z = 0;
ll bestfound = -(ll)1e18;
for (int k = 0; k < c[i]; k++) {
while (z < c[i - 1] && Y[i - 1][z].first < Y[i][k].first) {
bestfound = max(bestfound, D[i - 1][z]);
z++;
}
below[k] = bestfound;
}
// compute above
z = c[i - 1] - 1;
bestfound = -(ll)1e18;
for (int k = c[i] - 1; k >= 0; k--) {
while (z >= 0 && Y[i - 1][z].first > Y[i][k].first) {
bestfound = max(bestfound, F[i - 1][z]);
z--;
}
above[k] = bestfound;
}
// compute D:
ll bestF = -(ll)1e18;
if (c[i - 1])
bestF = *max_element(F[i - 1].begin(), F[i - 1].end());
D[i][0] = max(max(below[0], E[i - 1]), bestF) + Y[i][0].second;
for (int j = 1; j < c[i]; j++) {
D[i][j] = max(below[j], D[i][j - 1]) + Y[i][j].second;
}
// compute F:
F[i][c[i] - 1] = max(E[i - 1], above[c[i] - 1]) + Y[i][c[i] - 1].second;
for (int j = c[i] - 2; j >= 0; j--) {
F[i][j] = max(above[j], F[i][j + 1]) + Y[i][j].second;
}
/*
for (int j = 0; j < c[i]; j++) {
cout << "D[i][" << j << "] " << D[i][j] << '\n';
}
for (int j = 0; j < c[i]; j++) {
cout << "F[i][" << j << "] " << F[i][j] << '\n';
}
*/
}
ll result = E[n - 1];
for (int j = 0; j < c[n - 1]; j++) {
result = max(result, F[n - 1][j]);
}
return result;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
33 ms |
12324 KB |
Output is correct |
2 |
Correct |
40 ms |
13260 KB |
Output is correct |
3 |
Correct |
5 ms |
8404 KB |
Output is correct |
4 |
Correct |
4 ms |
8404 KB |
Output is correct |
5 |
Correct |
121 ms |
23956 KB |
Output is correct |
6 |
Correct |
134 ms |
25328 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
7252 KB |
Output is correct |
2 |
Correct |
76 ms |
16700 KB |
Output is correct |
3 |
Correct |
79 ms |
18904 KB |
Output is correct |
4 |
Correct |
34 ms |
12292 KB |
Output is correct |
5 |
Correct |
45 ms |
13248 KB |
Output is correct |
6 |
Correct |
4 ms |
7252 KB |
Output is correct |
7 |
Correct |
4 ms |
7252 KB |
Output is correct |
8 |
Correct |
4 ms |
7328 KB |
Output is correct |
9 |
Correct |
4 ms |
7252 KB |
Output is correct |
10 |
Correct |
5 ms |
8404 KB |
Output is correct |
11 |
Correct |
5 ms |
8404 KB |
Output is correct |
12 |
Correct |
34 ms |
12924 KB |
Output is correct |
13 |
Correct |
40 ms |
14072 KB |
Output is correct |
14 |
Correct |
34 ms |
12376 KB |
Output is correct |
15 |
Correct |
39 ms |
12912 KB |
Output is correct |
16 |
Correct |
35 ms |
12352 KB |
Output is correct |
17 |
Correct |
50 ms |
12936 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
8404 KB |
Output is correct |
2 |
Correct |
4 ms |
8404 KB |
Output is correct |
3 |
Correct |
30 ms |
14676 KB |
Output is correct |
4 |
Correct |
23 ms |
12620 KB |
Output is correct |
5 |
Correct |
55 ms |
20188 KB |
Output is correct |
6 |
Correct |
50 ms |
20188 KB |
Output is correct |
7 |
Correct |
55 ms |
20212 KB |
Output is correct |
8 |
Correct |
54 ms |
20168 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
7348 KB |
Output is correct |
2 |
Correct |
4 ms |
7252 KB |
Output is correct |
3 |
Correct |
4 ms |
7252 KB |
Output is correct |
4 |
Correct |
4 ms |
7252 KB |
Output is correct |
5 |
Correct |
5 ms |
7252 KB |
Output is correct |
6 |
Correct |
4 ms |
7252 KB |
Output is correct |
7 |
Correct |
3 ms |
7252 KB |
Output is correct |
8 |
Correct |
4 ms |
7252 KB |
Output is correct |
9 |
Correct |
4 ms |
7380 KB |
Output is correct |
10 |
Correct |
5 ms |
7380 KB |
Output is correct |
11 |
Correct |
4 ms |
7380 KB |
Output is correct |
12 |
Correct |
4 ms |
7380 KB |
Output is correct |
13 |
Correct |
4 ms |
7252 KB |
Output is correct |
14 |
Correct |
6 ms |
7380 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
7348 KB |
Output is correct |
2 |
Correct |
4 ms |
7252 KB |
Output is correct |
3 |
Correct |
4 ms |
7252 KB |
Output is correct |
4 |
Correct |
4 ms |
7252 KB |
Output is correct |
5 |
Correct |
5 ms |
7252 KB |
Output is correct |
6 |
Correct |
4 ms |
7252 KB |
Output is correct |
7 |
Correct |
3 ms |
7252 KB |
Output is correct |
8 |
Correct |
4 ms |
7252 KB |
Output is correct |
9 |
Correct |
4 ms |
7380 KB |
Output is correct |
10 |
Correct |
5 ms |
7380 KB |
Output is correct |
11 |
Correct |
4 ms |
7380 KB |
Output is correct |
12 |
Correct |
4 ms |
7380 KB |
Output is correct |
13 |
Correct |
4 ms |
7252 KB |
Output is correct |
14 |
Correct |
6 ms |
7380 KB |
Output is correct |
15 |
Correct |
4 ms |
7380 KB |
Output is correct |
16 |
Correct |
5 ms |
7412 KB |
Output is correct |
17 |
Correct |
19 ms |
9568 KB |
Output is correct |
18 |
Correct |
20 ms |
9816 KB |
Output is correct |
19 |
Correct |
19 ms |
9812 KB |
Output is correct |
20 |
Correct |
19 ms |
9792 KB |
Output is correct |
21 |
Correct |
20 ms |
9800 KB |
Output is correct |
22 |
Correct |
35 ms |
12124 KB |
Output is correct |
23 |
Correct |
7 ms |
7732 KB |
Output is correct |
24 |
Correct |
16 ms |
8916 KB |
Output is correct |
25 |
Correct |
4 ms |
7380 KB |
Output is correct |
26 |
Correct |
7 ms |
7764 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
7348 KB |
Output is correct |
2 |
Correct |
4 ms |
7252 KB |
Output is correct |
3 |
Correct |
4 ms |
7252 KB |
Output is correct |
4 |
Correct |
4 ms |
7252 KB |
Output is correct |
5 |
Correct |
5 ms |
7252 KB |
Output is correct |
6 |
Correct |
4 ms |
7252 KB |
Output is correct |
7 |
Correct |
3 ms |
7252 KB |
Output is correct |
8 |
Correct |
4 ms |
7252 KB |
Output is correct |
9 |
Correct |
4 ms |
7380 KB |
Output is correct |
10 |
Correct |
5 ms |
7380 KB |
Output is correct |
11 |
Correct |
4 ms |
7380 KB |
Output is correct |
12 |
Correct |
4 ms |
7380 KB |
Output is correct |
13 |
Correct |
4 ms |
7252 KB |
Output is correct |
14 |
Correct |
6 ms |
7380 KB |
Output is correct |
15 |
Correct |
4 ms |
7380 KB |
Output is correct |
16 |
Correct |
5 ms |
7412 KB |
Output is correct |
17 |
Correct |
19 ms |
9568 KB |
Output is correct |
18 |
Correct |
20 ms |
9816 KB |
Output is correct |
19 |
Correct |
19 ms |
9812 KB |
Output is correct |
20 |
Correct |
19 ms |
9792 KB |
Output is correct |
21 |
Correct |
20 ms |
9800 KB |
Output is correct |
22 |
Correct |
35 ms |
12124 KB |
Output is correct |
23 |
Correct |
7 ms |
7732 KB |
Output is correct |
24 |
Correct |
16 ms |
8916 KB |
Output is correct |
25 |
Correct |
4 ms |
7380 KB |
Output is correct |
26 |
Correct |
7 ms |
7764 KB |
Output is correct |
27 |
Correct |
6 ms |
7636 KB |
Output is correct |
28 |
Correct |
97 ms |
18996 KB |
Output is correct |
29 |
Correct |
131 ms |
23348 KB |
Output is correct |
30 |
Correct |
108 ms |
22788 KB |
Output is correct |
31 |
Correct |
113 ms |
22744 KB |
Output is correct |
32 |
Correct |
104 ms |
24056 KB |
Output is correct |
33 |
Correct |
124 ms |
22916 KB |
Output is correct |
34 |
Correct |
111 ms |
22972 KB |
Output is correct |
35 |
Correct |
44 ms |
13588 KB |
Output is correct |
36 |
Correct |
108 ms |
23180 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
8404 KB |
Output is correct |
2 |
Correct |
4 ms |
8404 KB |
Output is correct |
3 |
Correct |
30 ms |
14676 KB |
Output is correct |
4 |
Correct |
23 ms |
12620 KB |
Output is correct |
5 |
Correct |
55 ms |
20188 KB |
Output is correct |
6 |
Correct |
50 ms |
20188 KB |
Output is correct |
7 |
Correct |
55 ms |
20212 KB |
Output is correct |
8 |
Correct |
54 ms |
20168 KB |
Output is correct |
9 |
Correct |
55 ms |
20224 KB |
Output is correct |
10 |
Correct |
46 ms |
14972 KB |
Output is correct |
11 |
Correct |
93 ms |
22564 KB |
Output is correct |
12 |
Correct |
3 ms |
7252 KB |
Output is correct |
13 |
Correct |
4 ms |
7252 KB |
Output is correct |
14 |
Correct |
4 ms |
7264 KB |
Output is correct |
15 |
Correct |
4 ms |
7252 KB |
Output is correct |
16 |
Correct |
5 ms |
7280 KB |
Output is correct |
17 |
Correct |
4 ms |
7252 KB |
Output is correct |
18 |
Correct |
5 ms |
8404 KB |
Output is correct |
19 |
Correct |
5 ms |
8416 KB |
Output is correct |
20 |
Correct |
6 ms |
8404 KB |
Output is correct |
21 |
Correct |
5 ms |
8404 KB |
Output is correct |
22 |
Correct |
56 ms |
17112 KB |
Output is correct |
23 |
Correct |
91 ms |
22512 KB |
Output is correct |
24 |
Correct |
96 ms |
22552 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
33 ms |
12324 KB |
Output is correct |
2 |
Correct |
40 ms |
13260 KB |
Output is correct |
3 |
Correct |
5 ms |
8404 KB |
Output is correct |
4 |
Correct |
4 ms |
8404 KB |
Output is correct |
5 |
Correct |
121 ms |
23956 KB |
Output is correct |
6 |
Correct |
134 ms |
25328 KB |
Output is correct |
7 |
Correct |
4 ms |
7252 KB |
Output is correct |
8 |
Correct |
76 ms |
16700 KB |
Output is correct |
9 |
Correct |
79 ms |
18904 KB |
Output is correct |
10 |
Correct |
34 ms |
12292 KB |
Output is correct |
11 |
Correct |
45 ms |
13248 KB |
Output is correct |
12 |
Correct |
4 ms |
7252 KB |
Output is correct |
13 |
Correct |
4 ms |
7252 KB |
Output is correct |
14 |
Correct |
4 ms |
7328 KB |
Output is correct |
15 |
Correct |
4 ms |
7252 KB |
Output is correct |
16 |
Correct |
5 ms |
8404 KB |
Output is correct |
17 |
Correct |
5 ms |
8404 KB |
Output is correct |
18 |
Correct |
34 ms |
12924 KB |
Output is correct |
19 |
Correct |
40 ms |
14072 KB |
Output is correct |
20 |
Correct |
34 ms |
12376 KB |
Output is correct |
21 |
Correct |
39 ms |
12912 KB |
Output is correct |
22 |
Correct |
35 ms |
12352 KB |
Output is correct |
23 |
Correct |
50 ms |
12936 KB |
Output is correct |
24 |
Correct |
6 ms |
8404 KB |
Output is correct |
25 |
Correct |
4 ms |
8404 KB |
Output is correct |
26 |
Correct |
30 ms |
14676 KB |
Output is correct |
27 |
Correct |
23 ms |
12620 KB |
Output is correct |
28 |
Correct |
55 ms |
20188 KB |
Output is correct |
29 |
Correct |
50 ms |
20188 KB |
Output is correct |
30 |
Correct |
55 ms |
20212 KB |
Output is correct |
31 |
Correct |
54 ms |
20168 KB |
Output is correct |
32 |
Correct |
5 ms |
7348 KB |
Output is correct |
33 |
Correct |
4 ms |
7252 KB |
Output is correct |
34 |
Correct |
4 ms |
7252 KB |
Output is correct |
35 |
Correct |
4 ms |
7252 KB |
Output is correct |
36 |
Correct |
5 ms |
7252 KB |
Output is correct |
37 |
Correct |
4 ms |
7252 KB |
Output is correct |
38 |
Correct |
3 ms |
7252 KB |
Output is correct |
39 |
Correct |
4 ms |
7252 KB |
Output is correct |
40 |
Correct |
4 ms |
7380 KB |
Output is correct |
41 |
Correct |
5 ms |
7380 KB |
Output is correct |
42 |
Correct |
4 ms |
7380 KB |
Output is correct |
43 |
Correct |
4 ms |
7380 KB |
Output is correct |
44 |
Correct |
4 ms |
7252 KB |
Output is correct |
45 |
Correct |
6 ms |
7380 KB |
Output is correct |
46 |
Correct |
4 ms |
7380 KB |
Output is correct |
47 |
Correct |
5 ms |
7412 KB |
Output is correct |
48 |
Correct |
19 ms |
9568 KB |
Output is correct |
49 |
Correct |
20 ms |
9816 KB |
Output is correct |
50 |
Correct |
19 ms |
9812 KB |
Output is correct |
51 |
Correct |
19 ms |
9792 KB |
Output is correct |
52 |
Correct |
20 ms |
9800 KB |
Output is correct |
53 |
Correct |
35 ms |
12124 KB |
Output is correct |
54 |
Correct |
7 ms |
7732 KB |
Output is correct |
55 |
Correct |
16 ms |
8916 KB |
Output is correct |
56 |
Correct |
4 ms |
7380 KB |
Output is correct |
57 |
Correct |
7 ms |
7764 KB |
Output is correct |
58 |
Correct |
6 ms |
7636 KB |
Output is correct |
59 |
Correct |
97 ms |
18996 KB |
Output is correct |
60 |
Correct |
131 ms |
23348 KB |
Output is correct |
61 |
Correct |
108 ms |
22788 KB |
Output is correct |
62 |
Correct |
113 ms |
22744 KB |
Output is correct |
63 |
Correct |
104 ms |
24056 KB |
Output is correct |
64 |
Correct |
124 ms |
22916 KB |
Output is correct |
65 |
Correct |
111 ms |
22972 KB |
Output is correct |
66 |
Correct |
44 ms |
13588 KB |
Output is correct |
67 |
Correct |
108 ms |
23180 KB |
Output is correct |
68 |
Correct |
55 ms |
20224 KB |
Output is correct |
69 |
Correct |
46 ms |
14972 KB |
Output is correct |
70 |
Correct |
93 ms |
22564 KB |
Output is correct |
71 |
Correct |
3 ms |
7252 KB |
Output is correct |
72 |
Correct |
4 ms |
7252 KB |
Output is correct |
73 |
Correct |
4 ms |
7264 KB |
Output is correct |
74 |
Correct |
4 ms |
7252 KB |
Output is correct |
75 |
Correct |
5 ms |
7280 KB |
Output is correct |
76 |
Correct |
4 ms |
7252 KB |
Output is correct |
77 |
Correct |
5 ms |
8404 KB |
Output is correct |
78 |
Correct |
5 ms |
8416 KB |
Output is correct |
79 |
Correct |
6 ms |
8404 KB |
Output is correct |
80 |
Correct |
5 ms |
8404 KB |
Output is correct |
81 |
Correct |
56 ms |
17112 KB |
Output is correct |
82 |
Correct |
91 ms |
22512 KB |
Output is correct |
83 |
Correct |
96 ms |
22552 KB |
Output is correct |
84 |
Correct |
117 ms |
23492 KB |
Output is correct |
85 |
Correct |
118 ms |
23428 KB |
Output is correct |
86 |
Correct |
152 ms |
27040 KB |
Output is correct |
87 |
Correct |
152 ms |
27696 KB |
Output is correct |
88 |
Correct |
4 ms |
7252 KB |
Output is correct |
89 |
Correct |
147 ms |
27664 KB |
Output is correct |
90 |
Correct |
139 ms |
25540 KB |
Output is correct |
91 |
Correct |
114 ms |
24672 KB |
Output is correct |