#include "fish.h"
#include <bits/stdc++.h>
using namespace std;
const int maxN = 1e5 + 20;
const long long inf = 1e18L + 20;
vector<pair<int, int>> col[maxN];
vector<long long> dp_down[maxN];
vector<long long> dp_up[maxN];
long long max_weights(int N, int M, vector<int> X, vector<int> Y, vector<int> W) {
for (int i = 0; i < M; i++) {
col[X[i]].emplace_back(Y[i], W[i]);
}
for (int i = 0; i < N; i++) {
col[i].emplace_back(-1, 0);
col[i].emplace_back(N, 0);
sort(col[i].begin(), col[i].end());
dp_down[i].resize(col[i].size(), (i ? -inf : 0));
dp_up[i].resize(col[i].size(), (i ? -inf: 0));
}
for (int i = 1; i < N; i++) {
int prv_sz = col[i - 1].size();
int cur_sz = col[i].size();
int pt = cur_sz - 1;
for (int j = prv_sz - 1; j >= 0; j--) {
while (col[i][pt].first > col[i - 1][j].first) {
pt--;
}
dp_down[i][pt] = max(dp_down[i][pt], max(dp_up[i - 1][j], dp_down[i - 1][j]) + (col[i][pt].first < col[i - 1][j].first ? col[i][pt].second : 0));
}
for (int j = cur_sz - 2; j >= 0; j--) {
dp_down[i][j] = max(dp_down[i][j], dp_down[i][j + 1] + col[i][j].second);
}
long long max_up = -inf;
long long max_down = -inf;
pt = -1;
for (int j = 0; j < cur_sz; j++) {
while (pt + 1 < prv_sz && col[i - 1][pt + 1].first < col[i][j].first) {
max_up = max(max_up, dp_up[i - 1][pt + 1]) + col[i - 1][pt + 1].second;
max_down = max(max_down, dp_down[i - 1][pt + 1]);
pt++;
}
if (pt + 1 < prv_sz && col[i - 1][pt + 1].first == col[i][j].first) {
dp_up[i][j] = max(dp_up[i][j], dp_up[i - 1][pt + 1]);
dp_up[i][j] = max(dp_up[i][j], dp_down[i - 1][pt + 1]);
}
dp_up[i][j] = max(dp_up[i][j], max_up);
dp_up[i][j] = max(dp_up[i][j], max_down);
}
}
long long res = 0;
for (int i = 0; i < (int)col[N - 1].size(); i++) {
res = max(res, dp_up[N - 1][i]);
res = max(res, dp_down[N - 1][i]);
}
return res;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
43 ms |
19548 KB |
Output is correct |
2 |
Correct |
51 ms |
21436 KB |
Output is correct |
3 |
Correct |
21 ms |
16628 KB |
Output is correct |
4 |
Correct |
21 ms |
16744 KB |
Output is correct |
5 |
Correct |
117 ms |
30740 KB |
Output is correct |
6 |
Correct |
125 ms |
31600 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
7252 KB |
Output is correct |
2 |
Correct |
70 ms |
26208 KB |
Output is correct |
3 |
Correct |
82 ms |
29520 KB |
Output is correct |
4 |
Correct |
45 ms |
20928 KB |
Output is correct |
5 |
Correct |
51 ms |
23104 KB |
Output is correct |
6 |
Correct |
3 ms |
7252 KB |
Output is correct |
7 |
Correct |
3 ms |
7348 KB |
Output is correct |
8 |
Correct |
3 ms |
7252 KB |
Output is correct |
9 |
Correct |
3 ms |
7348 KB |
Output is correct |
10 |
Correct |
20 ms |
16692 KB |
Output is correct |
11 |
Correct |
24 ms |
16740 KB |
Output is correct |
12 |
Correct |
51 ms |
21024 KB |
Output is correct |
13 |
Correct |
51 ms |
23088 KB |
Output is correct |
14 |
Correct |
43 ms |
20952 KB |
Output is correct |
15 |
Correct |
48 ms |
22468 KB |
Output is correct |
16 |
Correct |
44 ms |
20944 KB |
Output is correct |
17 |
Correct |
51 ms |
22516 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
25 ms |
16716 KB |
Output is correct |
2 |
Correct |
22 ms |
16692 KB |
Output is correct |
3 |
Correct |
34 ms |
17784 KB |
Output is correct |
4 |
Correct |
32 ms |
18140 KB |
Output is correct |
5 |
Correct |
49 ms |
20652 KB |
Output is correct |
6 |
Correct |
52 ms |
20632 KB |
Output is correct |
7 |
Correct |
49 ms |
20560 KB |
Output is correct |
8 |
Correct |
49 ms |
20556 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
7252 KB |
Output is correct |
2 |
Correct |
5 ms |
7252 KB |
Output is correct |
3 |
Correct |
4 ms |
7252 KB |
Output is correct |
4 |
Correct |
3 ms |
7252 KB |
Output is correct |
5 |
Correct |
3 ms |
7252 KB |
Output is correct |
6 |
Correct |
4 ms |
7352 KB |
Output is correct |
7 |
Correct |
3 ms |
7252 KB |
Output is correct |
8 |
Correct |
3 ms |
7348 KB |
Output is correct |
9 |
Correct |
4 ms |
7368 KB |
Output is correct |
10 |
Correct |
4 ms |
7508 KB |
Output is correct |
11 |
Correct |
4 ms |
7352 KB |
Output is correct |
12 |
Correct |
4 ms |
7356 KB |
Output is correct |
13 |
Correct |
4 ms |
7252 KB |
Output is correct |
14 |
Correct |
4 ms |
7384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
7252 KB |
Output is correct |
2 |
Correct |
5 ms |
7252 KB |
Output is correct |
3 |
Correct |
4 ms |
7252 KB |
Output is correct |
4 |
Correct |
3 ms |
7252 KB |
Output is correct |
5 |
Correct |
3 ms |
7252 KB |
Output is correct |
6 |
Correct |
4 ms |
7352 KB |
Output is correct |
7 |
Correct |
3 ms |
7252 KB |
Output is correct |
8 |
Correct |
3 ms |
7348 KB |
Output is correct |
9 |
Correct |
4 ms |
7368 KB |
Output is correct |
10 |
Correct |
4 ms |
7508 KB |
Output is correct |
11 |
Correct |
4 ms |
7352 KB |
Output is correct |
12 |
Correct |
4 ms |
7356 KB |
Output is correct |
13 |
Correct |
4 ms |
7252 KB |
Output is correct |
14 |
Correct |
4 ms |
7384 KB |
Output is correct |
15 |
Correct |
4 ms |
7344 KB |
Output is correct |
16 |
Correct |
4 ms |
7408 KB |
Output is correct |
17 |
Correct |
21 ms |
10308 KB |
Output is correct |
18 |
Correct |
16 ms |
10568 KB |
Output is correct |
19 |
Correct |
17 ms |
10580 KB |
Output is correct |
20 |
Correct |
16 ms |
10452 KB |
Output is correct |
21 |
Correct |
17 ms |
10452 KB |
Output is correct |
22 |
Correct |
29 ms |
13636 KB |
Output is correct |
23 |
Correct |
6 ms |
7868 KB |
Output is correct |
24 |
Correct |
12 ms |
9300 KB |
Output is correct |
25 |
Correct |
4 ms |
7380 KB |
Output is correct |
26 |
Correct |
6 ms |
7892 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
7252 KB |
Output is correct |
2 |
Correct |
5 ms |
7252 KB |
Output is correct |
3 |
Correct |
4 ms |
7252 KB |
Output is correct |
4 |
Correct |
3 ms |
7252 KB |
Output is correct |
5 |
Correct |
3 ms |
7252 KB |
Output is correct |
6 |
Correct |
4 ms |
7352 KB |
Output is correct |
7 |
Correct |
3 ms |
7252 KB |
Output is correct |
8 |
Correct |
3 ms |
7348 KB |
Output is correct |
9 |
Correct |
4 ms |
7368 KB |
Output is correct |
10 |
Correct |
4 ms |
7508 KB |
Output is correct |
11 |
Correct |
4 ms |
7352 KB |
Output is correct |
12 |
Correct |
4 ms |
7356 KB |
Output is correct |
13 |
Correct |
4 ms |
7252 KB |
Output is correct |
14 |
Correct |
4 ms |
7384 KB |
Output is correct |
15 |
Correct |
4 ms |
7344 KB |
Output is correct |
16 |
Correct |
4 ms |
7408 KB |
Output is correct |
17 |
Correct |
21 ms |
10308 KB |
Output is correct |
18 |
Correct |
16 ms |
10568 KB |
Output is correct |
19 |
Correct |
17 ms |
10580 KB |
Output is correct |
20 |
Correct |
16 ms |
10452 KB |
Output is correct |
21 |
Correct |
17 ms |
10452 KB |
Output is correct |
22 |
Correct |
29 ms |
13636 KB |
Output is correct |
23 |
Correct |
6 ms |
7868 KB |
Output is correct |
24 |
Correct |
12 ms |
9300 KB |
Output is correct |
25 |
Correct |
4 ms |
7380 KB |
Output is correct |
26 |
Correct |
6 ms |
7892 KB |
Output is correct |
27 |
Correct |
4 ms |
7764 KB |
Output is correct |
28 |
Correct |
69 ms |
22928 KB |
Output is correct |
29 |
Correct |
97 ms |
28356 KB |
Output is correct |
30 |
Correct |
95 ms |
28136 KB |
Output is correct |
31 |
Correct |
94 ms |
28128 KB |
Output is correct |
32 |
Correct |
90 ms |
28900 KB |
Output is correct |
33 |
Correct |
100 ms |
28056 KB |
Output is correct |
34 |
Correct |
94 ms |
28108 KB |
Output is correct |
35 |
Correct |
38 ms |
15492 KB |
Output is correct |
36 |
Correct |
92 ms |
28720 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
25 ms |
16716 KB |
Output is correct |
2 |
Correct |
22 ms |
16692 KB |
Output is correct |
3 |
Correct |
34 ms |
17784 KB |
Output is correct |
4 |
Correct |
32 ms |
18140 KB |
Output is correct |
5 |
Correct |
49 ms |
20652 KB |
Output is correct |
6 |
Correct |
52 ms |
20632 KB |
Output is correct |
7 |
Correct |
49 ms |
20560 KB |
Output is correct |
8 |
Correct |
49 ms |
20556 KB |
Output is correct |
9 |
Correct |
45 ms |
20556 KB |
Output is correct |
10 |
Correct |
41 ms |
18832 KB |
Output is correct |
11 |
Correct |
88 ms |
30444 KB |
Output is correct |
12 |
Correct |
4 ms |
7280 KB |
Output is correct |
13 |
Correct |
5 ms |
7252 KB |
Output is correct |
14 |
Correct |
5 ms |
7252 KB |
Output is correct |
15 |
Correct |
4 ms |
7348 KB |
Output is correct |
16 |
Correct |
3 ms |
7252 KB |
Output is correct |
17 |
Correct |
3 ms |
7252 KB |
Output is correct |
18 |
Correct |
19 ms |
16748 KB |
Output is correct |
19 |
Correct |
20 ms |
16636 KB |
Output is correct |
20 |
Correct |
20 ms |
16724 KB |
Output is correct |
21 |
Correct |
20 ms |
16720 KB |
Output is correct |
22 |
Correct |
66 ms |
23212 KB |
Output is correct |
23 |
Correct |
88 ms |
30428 KB |
Output is correct |
24 |
Correct |
90 ms |
31052 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
43 ms |
19548 KB |
Output is correct |
2 |
Correct |
51 ms |
21436 KB |
Output is correct |
3 |
Correct |
21 ms |
16628 KB |
Output is correct |
4 |
Correct |
21 ms |
16744 KB |
Output is correct |
5 |
Correct |
117 ms |
30740 KB |
Output is correct |
6 |
Correct |
125 ms |
31600 KB |
Output is correct |
7 |
Correct |
3 ms |
7252 KB |
Output is correct |
8 |
Correct |
70 ms |
26208 KB |
Output is correct |
9 |
Correct |
82 ms |
29520 KB |
Output is correct |
10 |
Correct |
45 ms |
20928 KB |
Output is correct |
11 |
Correct |
51 ms |
23104 KB |
Output is correct |
12 |
Correct |
3 ms |
7252 KB |
Output is correct |
13 |
Correct |
3 ms |
7348 KB |
Output is correct |
14 |
Correct |
3 ms |
7252 KB |
Output is correct |
15 |
Correct |
3 ms |
7348 KB |
Output is correct |
16 |
Correct |
20 ms |
16692 KB |
Output is correct |
17 |
Correct |
24 ms |
16740 KB |
Output is correct |
18 |
Correct |
51 ms |
21024 KB |
Output is correct |
19 |
Correct |
51 ms |
23088 KB |
Output is correct |
20 |
Correct |
43 ms |
20952 KB |
Output is correct |
21 |
Correct |
48 ms |
22468 KB |
Output is correct |
22 |
Correct |
44 ms |
20944 KB |
Output is correct |
23 |
Correct |
51 ms |
22516 KB |
Output is correct |
24 |
Correct |
25 ms |
16716 KB |
Output is correct |
25 |
Correct |
22 ms |
16692 KB |
Output is correct |
26 |
Correct |
34 ms |
17784 KB |
Output is correct |
27 |
Correct |
32 ms |
18140 KB |
Output is correct |
28 |
Correct |
49 ms |
20652 KB |
Output is correct |
29 |
Correct |
52 ms |
20632 KB |
Output is correct |
30 |
Correct |
49 ms |
20560 KB |
Output is correct |
31 |
Correct |
49 ms |
20556 KB |
Output is correct |
32 |
Correct |
3 ms |
7252 KB |
Output is correct |
33 |
Correct |
5 ms |
7252 KB |
Output is correct |
34 |
Correct |
4 ms |
7252 KB |
Output is correct |
35 |
Correct |
3 ms |
7252 KB |
Output is correct |
36 |
Correct |
3 ms |
7252 KB |
Output is correct |
37 |
Correct |
4 ms |
7352 KB |
Output is correct |
38 |
Correct |
3 ms |
7252 KB |
Output is correct |
39 |
Correct |
3 ms |
7348 KB |
Output is correct |
40 |
Correct |
4 ms |
7368 KB |
Output is correct |
41 |
Correct |
4 ms |
7508 KB |
Output is correct |
42 |
Correct |
4 ms |
7352 KB |
Output is correct |
43 |
Correct |
4 ms |
7356 KB |
Output is correct |
44 |
Correct |
4 ms |
7252 KB |
Output is correct |
45 |
Correct |
4 ms |
7384 KB |
Output is correct |
46 |
Correct |
4 ms |
7344 KB |
Output is correct |
47 |
Correct |
4 ms |
7408 KB |
Output is correct |
48 |
Correct |
21 ms |
10308 KB |
Output is correct |
49 |
Correct |
16 ms |
10568 KB |
Output is correct |
50 |
Correct |
17 ms |
10580 KB |
Output is correct |
51 |
Correct |
16 ms |
10452 KB |
Output is correct |
52 |
Correct |
17 ms |
10452 KB |
Output is correct |
53 |
Correct |
29 ms |
13636 KB |
Output is correct |
54 |
Correct |
6 ms |
7868 KB |
Output is correct |
55 |
Correct |
12 ms |
9300 KB |
Output is correct |
56 |
Correct |
4 ms |
7380 KB |
Output is correct |
57 |
Correct |
6 ms |
7892 KB |
Output is correct |
58 |
Correct |
4 ms |
7764 KB |
Output is correct |
59 |
Correct |
69 ms |
22928 KB |
Output is correct |
60 |
Correct |
97 ms |
28356 KB |
Output is correct |
61 |
Correct |
95 ms |
28136 KB |
Output is correct |
62 |
Correct |
94 ms |
28128 KB |
Output is correct |
63 |
Correct |
90 ms |
28900 KB |
Output is correct |
64 |
Correct |
100 ms |
28056 KB |
Output is correct |
65 |
Correct |
94 ms |
28108 KB |
Output is correct |
66 |
Correct |
38 ms |
15492 KB |
Output is correct |
67 |
Correct |
92 ms |
28720 KB |
Output is correct |
68 |
Correct |
45 ms |
20556 KB |
Output is correct |
69 |
Correct |
41 ms |
18832 KB |
Output is correct |
70 |
Correct |
88 ms |
30444 KB |
Output is correct |
71 |
Correct |
4 ms |
7280 KB |
Output is correct |
72 |
Correct |
5 ms |
7252 KB |
Output is correct |
73 |
Correct |
5 ms |
7252 KB |
Output is correct |
74 |
Correct |
4 ms |
7348 KB |
Output is correct |
75 |
Correct |
3 ms |
7252 KB |
Output is correct |
76 |
Correct |
3 ms |
7252 KB |
Output is correct |
77 |
Correct |
19 ms |
16748 KB |
Output is correct |
78 |
Correct |
20 ms |
16636 KB |
Output is correct |
79 |
Correct |
20 ms |
16724 KB |
Output is correct |
80 |
Correct |
20 ms |
16720 KB |
Output is correct |
81 |
Correct |
66 ms |
23212 KB |
Output is correct |
82 |
Correct |
88 ms |
30428 KB |
Output is correct |
83 |
Correct |
90 ms |
31052 KB |
Output is correct |
84 |
Correct |
139 ms |
36380 KB |
Output is correct |
85 |
Correct |
120 ms |
36612 KB |
Output is correct |
86 |
Correct |
122 ms |
36676 KB |
Output is correct |
87 |
Correct |
135 ms |
37612 KB |
Output is correct |
88 |
Correct |
4 ms |
7252 KB |
Output is correct |
89 |
Correct |
129 ms |
37604 KB |
Output is correct |
90 |
Correct |
128 ms |
36832 KB |
Output is correct |
91 |
Correct |
117 ms |
36968 KB |
Output is correct |