#include "fish.h"
#include <vector>
#include <bits/stdc++.h>
using namespace std;
#ifdef DEBUG
#include "/home/eagle/ioi22/debug.h"
#else
#define debug(...) void(37)
#endif
long long max_weights(int N, int M, std::vector<int> X, std::vector<int> Y, std::vector<int> W) {
vector<int> ord(M);
iota(ord.begin(), ord.end(), 0);
sort(ord.begin(), ord.end(), [&](int x, int y) {
return pair{X[x], Y[x]} < pair{X[y], Y[y]};
});
vector<pair<int, vector<array<int, 2>>>> gs;
vector<vector<long long>> pref;
gs.emplace_back(-1, vector<array<int, 2>>{});
pref.push_back(vector<long long>{0});
for (int i = 0; i < M; ++i) {
if (i == 0 || X[ord[i - 1]] != X[ord[i]]) {
gs.emplace_back(X[ord[i]], vector<array<int, 2>>{});
pref.push_back(vector<long long>{0});
}
gs.back().second.push_back({Y[ord[i]], W[ord[i]]});
pref.back().push_back(pref.back().back() + W[ord[i]]);
}
gs.emplace_back(N, vector<array<int, 2>>{});
pref.push_back(vector<long long>{0});
debug(gs, pref);
const long long inf = (long long) 1e16;
vector<long long> inc(1, -inf);
vector<long long> dec(1, -inf);
vector<long long> valley(1, -inf);
long long ans = 0;
for (int i = 1; i < int(gs.size()); ++i) {
auto& a = gs[i].second;
//auto& next = gs[i + 1].second;
auto& prev = gs[i - 1].second;
int s = int(a.size());
debug(a, prev);
//int next_s = int(next.size());
int prev_s = int(prev.size());
vector<long long> new_inc(s + 1);
vector<long long> new_dec(s + 1);
vector<long long> new_valley(s + 1);
vector<int> need(s + 1);
int fop = -1;
for (int j = 0; j < s; ++j) {
while (fop + 1 < prev_s && prev[fop + 1][0] <= a[j][0]) {
++fop;
}
need[j + 1] = fop + 1;
}
vector<int> takes(s + 1);
{
int p = 0;
for (int j = 0; j < s; ++j) {
while (p < prev_s && prev[p][0] < a[j][0]) {
++p;
}
takes[j] = p;
}
}
takes.back() = prev_s;
debug(need, takes);
if (gs[i].first != gs[i - 1].first + 1) {
long long mx = 0;
for (auto x : valley) {
mx = max(mx, x + pref[i - 1].back());
}
for (int j = 0; j <= prev_s; ++j) {
mx = max(mx, pref[i - 1].back() - pref[i - 1][j] + inc[j]);
}
ans += mx;
for (int j = 0; j <= s; ++j) {
new_valley[j] = 0;
}
for (int j = 0; j <= s; ++j) {
new_dec[j] = pref[i].back() - pref[i][j];
}
for (int j = 0; j <= s; ++j) {
new_inc[j] = 0;
}
debug(ans, new_valley, new_dec, new_inc);
} else {
//valley case
{
int p = prev_s;
long long mx = -inf;
for (int j = s; j >= 0; --j) {
while (p >= need[j]) {
mx = max({mx, dec[p], inc[p]});
--p;
}
new_valley[j] = mx;
}
}
//inc
{
{
long long mx = 0;
long long val = 0;
int p = 0;
for (int j = 0; j <= s; ++j) {
while (p <= takes[j]) {
mx = max(mx, inc[p] - pref[i - 1][p]);
val = max(val, valley[p]);
++p;
}
new_inc[j] = max(new_inc[j], max(val, mx) + pref[i - 1][takes[j]]);
}
}
debug(new_inc);
{
long long down = 0;
for (int j = 0; j <= prev_s; ++j) {
down = max(down, pref[i - 1][j] + valley[j]);
}
for (int j = 0; j <= s; ++j) {
new_inc[j] = max(new_inc[j], down);
}
}
}
//dec
{
int p = prev_s;
long long prev_mx = -inf;
for (int j = s; j >= 0; --j) {
while (p >= need[j]) {
prev_mx = max(prev_mx, max(inc[p], dec[p]) + pref[i][j]);
--p;
}
new_dec[j] = max(0LL, prev_mx - pref[i][j]);
}
}
debug(new_valley);
debug(new_dec);
debug(new_inc);
}
swap(inc, new_inc);
swap(dec, new_dec);
swap(valley, new_valley);
}
//dont forget the end case
debug(ans);
return ans + dec[0];
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
28 ms |
6200 KB |
Output is correct |
2 |
Correct |
35 ms |
7840 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
216 KB |
Output is correct |
5 |
Correct |
111 ms |
19496 KB |
Output is correct |
6 |
Correct |
162 ms |
19820 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
59 ms |
11684 KB |
Output is correct |
3 |
Correct |
73 ms |
14436 KB |
Output is correct |
4 |
Correct |
37 ms |
6348 KB |
Output is correct |
5 |
Correct |
34 ms |
7864 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 |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
27 ms |
6424 KB |
Output is correct |
13 |
Correct |
34 ms |
7752 KB |
Output is correct |
14 |
Correct |
28 ms |
6340 KB |
Output is correct |
15 |
Correct |
32 ms |
6976 KB |
Output is correct |
16 |
Correct |
28 ms |
6336 KB |
Output is correct |
17 |
Correct |
32 ms |
6888 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
30 ms |
8176 KB |
Output is correct |
4 |
Correct |
21 ms |
6416 KB |
Output is correct |
5 |
Correct |
55 ms |
14900 KB |
Output is correct |
6 |
Correct |
62 ms |
14824 KB |
Output is correct |
7 |
Correct |
69 ms |
14896 KB |
Output is correct |
8 |
Correct |
60 ms |
14908 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 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 |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
2 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 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 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 |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
2 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 |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
17 ms |
2340 KB |
Output is correct |
18 |
Correct |
17 ms |
3644 KB |
Output is correct |
19 |
Correct |
16 ms |
3508 KB |
Output is correct |
20 |
Correct |
20 ms |
3540 KB |
Output is correct |
21 |
Correct |
16 ms |
3436 KB |
Output is correct |
22 |
Correct |
33 ms |
6732 KB |
Output is correct |
23 |
Correct |
4 ms |
932 KB |
Output is correct |
24 |
Correct |
12 ms |
2344 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
4 ms |
852 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 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 |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
2 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 |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
17 ms |
2340 KB |
Output is correct |
18 |
Correct |
17 ms |
3644 KB |
Output is correct |
19 |
Correct |
16 ms |
3508 KB |
Output is correct |
20 |
Correct |
20 ms |
3540 KB |
Output is correct |
21 |
Correct |
16 ms |
3436 KB |
Output is correct |
22 |
Correct |
33 ms |
6732 KB |
Output is correct |
23 |
Correct |
4 ms |
932 KB |
Output is correct |
24 |
Correct |
12 ms |
2344 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
4 ms |
852 KB |
Output is correct |
27 |
Correct |
2 ms |
752 KB |
Output is correct |
28 |
Correct |
83 ms |
11972 KB |
Output is correct |
29 |
Correct |
124 ms |
14396 KB |
Output is correct |
30 |
Correct |
124 ms |
14816 KB |
Output is correct |
31 |
Correct |
127 ms |
14796 KB |
Output is correct |
32 |
Correct |
110 ms |
17320 KB |
Output is correct |
33 |
Correct |
126 ms |
14908 KB |
Output is correct |
34 |
Correct |
130 ms |
14840 KB |
Output is correct |
35 |
Correct |
48 ms |
6432 KB |
Output is correct |
36 |
Correct |
124 ms |
16104 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
30 ms |
8176 KB |
Output is correct |
4 |
Correct |
21 ms |
6416 KB |
Output is correct |
5 |
Correct |
55 ms |
14900 KB |
Output is correct |
6 |
Correct |
62 ms |
14824 KB |
Output is correct |
7 |
Correct |
69 ms |
14896 KB |
Output is correct |
8 |
Correct |
60 ms |
14908 KB |
Output is correct |
9 |
Correct |
56 ms |
14888 KB |
Output is correct |
10 |
Correct |
50 ms |
9744 KB |
Output is correct |
11 |
Correct |
102 ms |
19144 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
292 KB |
Output is correct |
15 |
Correct |
0 ms |
300 KB |
Output is correct |
16 |
Correct |
0 ms |
300 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
0 ms |
212 KB |
Output is correct |
22 |
Correct |
65 ms |
13416 KB |
Output is correct |
23 |
Correct |
121 ms |
19232 KB |
Output is correct |
24 |
Correct |
105 ms |
19180 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
28 ms |
6200 KB |
Output is correct |
2 |
Correct |
35 ms |
7840 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
216 KB |
Output is correct |
5 |
Correct |
111 ms |
19496 KB |
Output is correct |
6 |
Correct |
162 ms |
19820 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
59 ms |
11684 KB |
Output is correct |
9 |
Correct |
73 ms |
14436 KB |
Output is correct |
10 |
Correct |
37 ms |
6348 KB |
Output is correct |
11 |
Correct |
34 ms |
7864 KB |
Output is correct |
12 |
Correct |
0 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 |
340 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
27 ms |
6424 KB |
Output is correct |
19 |
Correct |
34 ms |
7752 KB |
Output is correct |
20 |
Correct |
28 ms |
6340 KB |
Output is correct |
21 |
Correct |
32 ms |
6976 KB |
Output is correct |
22 |
Correct |
28 ms |
6336 KB |
Output is correct |
23 |
Correct |
32 ms |
6888 KB |
Output is correct |
24 |
Correct |
0 ms |
212 KB |
Output is correct |
25 |
Correct |
0 ms |
212 KB |
Output is correct |
26 |
Correct |
30 ms |
8176 KB |
Output is correct |
27 |
Correct |
21 ms |
6416 KB |
Output is correct |
28 |
Correct |
55 ms |
14900 KB |
Output is correct |
29 |
Correct |
62 ms |
14824 KB |
Output is correct |
30 |
Correct |
69 ms |
14896 KB |
Output is correct |
31 |
Correct |
60 ms |
14908 KB |
Output is correct |
32 |
Correct |
0 ms |
212 KB |
Output is correct |
33 |
Correct |
0 ms |
212 KB |
Output is correct |
34 |
Correct |
0 ms |
212 KB |
Output is correct |
35 |
Correct |
0 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 |
1 ms |
212 KB |
Output is correct |
40 |
Correct |
1 ms |
340 KB |
Output is correct |
41 |
Correct |
2 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 |
0 ms |
340 KB |
Output is correct |
47 |
Correct |
1 ms |
340 KB |
Output is correct |
48 |
Correct |
17 ms |
2340 KB |
Output is correct |
49 |
Correct |
17 ms |
3644 KB |
Output is correct |
50 |
Correct |
16 ms |
3508 KB |
Output is correct |
51 |
Correct |
20 ms |
3540 KB |
Output is correct |
52 |
Correct |
16 ms |
3436 KB |
Output is correct |
53 |
Correct |
33 ms |
6732 KB |
Output is correct |
54 |
Correct |
4 ms |
932 KB |
Output is correct |
55 |
Correct |
12 ms |
2344 KB |
Output is correct |
56 |
Correct |
1 ms |
340 KB |
Output is correct |
57 |
Correct |
4 ms |
852 KB |
Output is correct |
58 |
Correct |
2 ms |
752 KB |
Output is correct |
59 |
Correct |
83 ms |
11972 KB |
Output is correct |
60 |
Correct |
124 ms |
14396 KB |
Output is correct |
61 |
Correct |
124 ms |
14816 KB |
Output is correct |
62 |
Correct |
127 ms |
14796 KB |
Output is correct |
63 |
Correct |
110 ms |
17320 KB |
Output is correct |
64 |
Correct |
126 ms |
14908 KB |
Output is correct |
65 |
Correct |
130 ms |
14840 KB |
Output is correct |
66 |
Correct |
48 ms |
6432 KB |
Output is correct |
67 |
Correct |
124 ms |
16104 KB |
Output is correct |
68 |
Correct |
56 ms |
14888 KB |
Output is correct |
69 |
Correct |
50 ms |
9744 KB |
Output is correct |
70 |
Correct |
102 ms |
19144 KB |
Output is correct |
71 |
Correct |
0 ms |
212 KB |
Output is correct |
72 |
Correct |
0 ms |
212 KB |
Output is correct |
73 |
Correct |
1 ms |
292 KB |
Output is correct |
74 |
Correct |
0 ms |
300 KB |
Output is correct |
75 |
Correct |
0 ms |
300 KB |
Output is correct |
76 |
Correct |
1 ms |
212 KB |
Output is correct |
77 |
Correct |
1 ms |
212 KB |
Output is correct |
78 |
Correct |
0 ms |
212 KB |
Output is correct |
79 |
Correct |
0 ms |
212 KB |
Output is correct |
80 |
Correct |
0 ms |
212 KB |
Output is correct |
81 |
Correct |
65 ms |
13416 KB |
Output is correct |
82 |
Correct |
121 ms |
19232 KB |
Output is correct |
83 |
Correct |
105 ms |
19180 KB |
Output is correct |
84 |
Correct |
119 ms |
14540 KB |
Output is correct |
85 |
Correct |
120 ms |
14680 KB |
Output is correct |
86 |
Correct |
154 ms |
22844 KB |
Output is correct |
87 |
Correct |
164 ms |
23708 KB |
Output is correct |
88 |
Correct |
0 ms |
212 KB |
Output is correct |
89 |
Correct |
158 ms |
23704 KB |
Output is correct |
90 |
Correct |
143 ms |
19344 KB |
Output is correct |
91 |
Correct |
160 ms |
17660 KB |
Output is correct |