#include <bits/stdc++.h>
long long max_weights(int n, int m, std::vector <int> _x, std::vector <int> _y, std::vector <int> _w) {
std::vector <std::vector <std::pair <int, int>>> of(n);
for (int i = 0; i < m; i++) {
of[_x[i]].emplace_back(_y[i], _w[i]);
}
for (int i = 0; i < n; i++) {
of[i].emplace_back(0, 0);
of[i].emplace_back(n, 0);
std::sort(of[i].begin(), of[i].end());
if (!of[i][1].first) {
of[i].erase(of[i].begin());
}
}
std::vector <std::vector <std::vector <long long>>> dp(n, std::vector <std::vector <long long>> (3));
for (int i = 0; i < n; i++) {
for (int j = 0; j < 3; j++) {
dp[i][j].resize(of[i].size());
}
}
std::vector <std::vector <long long>> pre(n);
for (int i = 0; i < n; i++) {
pre[i].resize(of[i].size());
pre[i][0] = of[i][0].second;
for (int j = 1; j < (int) of[i].size(); j++) {
pre[i][j] = pre[i][j - 1] + of[i][j].second;
}
}
dp[0][0].assign(dp[0][0].size(), 0);
for (int nxt = 0, j = 1; j < (int) of[0].size(); j++) {
while (nxt + 1 < (int) of[1].size() && of[1][nxt + 1].first < of[0][j].first) {
nxt++;
}
dp[0][1][j] = pre[1][nxt];
}
for (int i = 1; i < n; i++) {
std::vector <long long> pre_mx0(dp[i - 1][0].size());
pre_mx0[0] = dp[i - 1][0][0];
for (int j = 1; j < (int) dp[i - 1][0].size(); j++) {
pre_mx0[j] = std::max(dp[i - 1][0][j], pre_mx0[j - 1]);
}
std::vector <long long> suf_mx[2];
for (int k = 0; k <= 1; k++) {
suf_mx[k].resize(dp[i - 1][k + 1].size());
suf_mx[k].back() = dp[i - 1][k + 1].back();
for (int j = (int) dp[i - 1][k + 1].size() - 2; j >= 0; j--) {
suf_mx[k][j] = std::max(dp[i - 1][k + 1][j], suf_mx[k][j + 1]);
}
}
std::vector <long long> psuf_mx[2];
std::vector <long long> ppre_mx[2];
for (int k = 0; k <= 1; k++) {
psuf_mx[k].resize(i > 1 ? dp[i - 2][k + 1].size() : 1, 0);
if (i > 1) {
ppre_mx[k].resize(dp[i - 2][k + 1].size());
for (int ptr = 0, j = 0; j < (int) dp[i - 2][k + 1].size(); j++) {
while (ptr + 1 < (int) of[i - 1].size() && of[i - 1][ptr + 1].first < of[i - 2][j].first) {
ptr++;
}
ppre_mx[k][j] = dp[i - 2][k + 1][j] - pre[i - 1][ptr];
if (j) {
ppre_mx[k][j] = std::max(ppre_mx[k][j], ppre_mx[k][j - 1]);
}
}
psuf_mx[k].back() = dp[i - 2][k + 1].back();
for (int j = (int) dp[i - 2][k + 1].size() - 2; j >= 0; j--) {
psuf_mx[k][j] = std::max(dp[i - 2][k + 1][j], psuf_mx[k][j + 1]);
}
}
}
dp[i][0][0] = dp[i][0][0] = std::max({ pre_mx0[0], psuf_mx[0][0], psuf_mx[1][0] });
dp[i][1][0] = pre_mx0[0];
dp[i][2][0] = std::max(suf_mx[0][0], suf_mx[1][0]);
if (i > 1) {
for (int prv = 0, nxt = 0, nnxt = 0, j = 0; j < (int) dp[i][0].size(); j++) {
while (prv + 1 < (int) of[i - 2].size() && of[i - 2][prv + 1].first < of[i][j].first) {
prv++;
}
while (nxt + 1 < (int) of[i - 1].size() && of[i - 1][nxt + 1].first < of[i][j].first) {
nxt++;
}
while (i + 1 < n && nnxt + 1 < (int) of[i + 1].size() && of[i + 1][nnxt + 1].first < of[i][j].first) {
nnxt++;
}
dp[i][0][j] = std::max({ dp[i][0][j], ppre_mx[0][prv] + pre[i - 1][nxt],
ppre_mx[1][prv] + pre[i - 1][nxt], psuf_mx[0][prv], psuf_mx[1][prv] });
dp[i][1][j] = std::max(dp[i][1][j], std::max({ ppre_mx[0][prv] + pre[i - 1][nxt],
ppre_mx[1][prv] + pre[i - 1][nxt], psuf_mx[0][prv], psuf_mx[1][prv] }) + (i + 1 < n ? pre[i + 1][nnxt] : 0LL));
}
}
for (int prv = 0, nxt = 0, j = 1; j < (int) of[i].size(); j++) {
while (prv + 1 < (int) of[i - 1].size() && of[i - 1][prv + 1].first < of[i][j].first) {
prv++;
}
while (i + 1 < n && nxt + 1 < (int) of[i + 1].size() && of[i + 1][nxt + 1].first < of[i][j].first) {
nxt++;
}
dp[i][0][j] = std::max(dp[i][0][j], pre_mx0[prv] + pre[i - 1][prv] - pre[i][j - 1]);
dp[i][1][j] = std::max(dp[i][1][j], pre_mx0[prv] + pre[i - 1][prv] + (i + 1 < n ? pre[i + 1][nxt] : 0LL));
dp[i][2][j] = std::max(
suf_mx[0][prv] + (i + 1 < n ? pre[i + 1][nxt] : 0LL) - pre[i][j - 1],
suf_mx[1][prv] + (i + 1 < n ? pre[i + 1][nxt] : 0LL) - pre[i][j - 1]);
}
}
long long ans = 0;
for (int k = 0; k < 3; k++) {
for (long long x : dp[n - 1][k]) {
ans = std::max(ans, x);
}
}
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
108 ms |
35344 KB |
Output is correct |
2 |
Correct |
162 ms |
40204 KB |
Output is correct |
3 |
Correct |
119 ms |
30784 KB |
Output is correct |
4 |
Correct |
80 ms |
30712 KB |
Output is correct |
5 |
Correct |
250 ms |
52496 KB |
Output is correct |
6 |
Correct |
256 ms |
50148 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
158 ms |
42296 KB |
Output is correct |
3 |
Correct |
193 ms |
48780 KB |
Output is correct |
4 |
Correct |
110 ms |
35332 KB |
Output is correct |
5 |
Correct |
129 ms |
40196 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 |
89 ms |
30824 KB |
Output is correct |
11 |
Correct |
86 ms |
30804 KB |
Output is correct |
12 |
Correct |
130 ms |
35500 KB |
Output is correct |
13 |
Correct |
125 ms |
40124 KB |
Output is correct |
14 |
Correct |
107 ms |
35000 KB |
Output is correct |
15 |
Correct |
131 ms |
38912 KB |
Output is correct |
16 |
Correct |
123 ms |
35108 KB |
Output is correct |
17 |
Correct |
155 ms |
38808 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
106 ms |
30756 KB |
Output is correct |
2 |
Correct |
93 ms |
30816 KB |
Output is correct |
3 |
Correct |
109 ms |
29976 KB |
Output is correct |
4 |
Correct |
115 ms |
32204 KB |
Output is correct |
5 |
Correct |
165 ms |
35224 KB |
Output is correct |
6 |
Correct |
140 ms |
35256 KB |
Output is correct |
7 |
Correct |
142 ms |
35240 KB |
Output is correct |
8 |
Correct |
146 ms |
35268 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 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 |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Incorrect |
1 ms |
340 KB |
1st lines differ - on the 1st token, expected: '216624184325', found: '220546644089' |
10 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 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 |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Incorrect |
1 ms |
340 KB |
1st lines differ - on the 1st token, expected: '216624184325', found: '220546644089' |
10 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 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 |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Incorrect |
1 ms |
340 KB |
1st lines differ - on the 1st token, expected: '216624184325', found: '220546644089' |
10 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
106 ms |
30756 KB |
Output is correct |
2 |
Correct |
93 ms |
30816 KB |
Output is correct |
3 |
Correct |
109 ms |
29976 KB |
Output is correct |
4 |
Correct |
115 ms |
32204 KB |
Output is correct |
5 |
Correct |
165 ms |
35224 KB |
Output is correct |
6 |
Correct |
140 ms |
35256 KB |
Output is correct |
7 |
Correct |
142 ms |
35240 KB |
Output is correct |
8 |
Correct |
146 ms |
35268 KB |
Output is correct |
9 |
Correct |
146 ms |
35264 KB |
Output is correct |
10 |
Correct |
92 ms |
18844 KB |
Output is correct |
11 |
Correct |
194 ms |
37616 KB |
Output is correct |
12 |
Correct |
1 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 |
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 |
81 ms |
30812 KB |
Output is correct |
19 |
Correct |
85 ms |
30820 KB |
Output is correct |
20 |
Correct |
100 ms |
30792 KB |
Output is correct |
21 |
Correct |
91 ms |
30792 KB |
Output is correct |
22 |
Incorrect |
152 ms |
36428 KB |
1st lines differ - on the 1st token, expected: '45561826463480', found: '46009932967206' |
23 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
108 ms |
35344 KB |
Output is correct |
2 |
Correct |
162 ms |
40204 KB |
Output is correct |
3 |
Correct |
119 ms |
30784 KB |
Output is correct |
4 |
Correct |
80 ms |
30712 KB |
Output is correct |
5 |
Correct |
250 ms |
52496 KB |
Output is correct |
6 |
Correct |
256 ms |
50148 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
158 ms |
42296 KB |
Output is correct |
9 |
Correct |
193 ms |
48780 KB |
Output is correct |
10 |
Correct |
110 ms |
35332 KB |
Output is correct |
11 |
Correct |
129 ms |
40196 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 |
89 ms |
30824 KB |
Output is correct |
17 |
Correct |
86 ms |
30804 KB |
Output is correct |
18 |
Correct |
130 ms |
35500 KB |
Output is correct |
19 |
Correct |
125 ms |
40124 KB |
Output is correct |
20 |
Correct |
107 ms |
35000 KB |
Output is correct |
21 |
Correct |
131 ms |
38912 KB |
Output is correct |
22 |
Correct |
123 ms |
35108 KB |
Output is correct |
23 |
Correct |
155 ms |
38808 KB |
Output is correct |
24 |
Correct |
106 ms |
30756 KB |
Output is correct |
25 |
Correct |
93 ms |
30816 KB |
Output is correct |
26 |
Correct |
109 ms |
29976 KB |
Output is correct |
27 |
Correct |
115 ms |
32204 KB |
Output is correct |
28 |
Correct |
165 ms |
35224 KB |
Output is correct |
29 |
Correct |
140 ms |
35256 KB |
Output is correct |
30 |
Correct |
142 ms |
35240 KB |
Output is correct |
31 |
Correct |
146 ms |
35268 KB |
Output is correct |
32 |
Correct |
1 ms |
212 KB |
Output is correct |
33 |
Correct |
1 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 |
1 ms |
212 KB |
Output is correct |
37 |
Correct |
0 ms |
212 KB |
Output is correct |
38 |
Correct |
1 ms |
212 KB |
Output is correct |
39 |
Correct |
1 ms |
212 KB |
Output is correct |
40 |
Incorrect |
1 ms |
340 KB |
1st lines differ - on the 1st token, expected: '216624184325', found: '220546644089' |
41 |
Halted |
0 ms |
0 KB |
- |