#include "fish.h"
#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int N = 3e5 + 10;
int sz[N];
vector<int> v[N];
vector<pair<int, int>> my[N];
vector<ll> dpinc[N];
vector<ll> dpdec[N];
vector<ll> pref[N];
long long max_weights(int n, int m, std::vector<int> x, std::vector<int> y,
std::vector<int> w) {
for (int i = 1; i <= n; i++)
v[i].push_back(0);
for (int i = 0; i < m; i++) {
x[i]++; y[i]++;
v[x[i] - 1].push_back(y[i]);
v[x[i] + 1].push_back(y[i]);
v[x[i]].push_back(y[i]);
my[x[i]].emplace_back(y[i], w[i]);
}
for (int i = 1; i <= n; i++) {
sort(v[i].begin(), v[i].end());
v[i].erase(unique(v[i].begin(), v[i].end()), v[i].end());
sz[i] = (int) v[i].size();
dpinc[i].resize(sz[i]);
dpdec[i].resize(sz[i]);
pref[i].resize(sz[i]);
for (auto& p : my[i]) {
int idx = (int) (lower_bound(v[i].begin(), v[i].end(), p.first) - v[i].begin());
pref[i][idx] += p.second;
}
for (int j = 1; j < sz[i]; j++) pref[i][j] += pref[i][j - 1];
}
{
// build dpdec 1
int ptr = 0;
ll s = 0;
for (int j = 0; j < sz[1]; j++) {
while (ptr < sz[2] && v[2][ptr] <= v[1][j]) {
s = pref[2][ptr];
ptr++;
}
dpdec[1][j] = s;
}
}
for (int i = 2; i <= n; i++) {
{
// inc -> inc
ll mx = 0;
ll s = 0;
int ptr = 0;
for (int j = 0; j < sz[i]; j++) {
while (ptr < sz[i - 1] && v[i - 1][ptr] <= v[i][j]) {
s = pref[i - 1][ptr];
mx = max(mx, dpinc[i - 1][ptr] - pref[i - 1][ptr]);
ptr++;
}
dpinc[i][j] = mx + s;
}
}
{
// dec -> dec
ll mx = 0;
int ptr = sz[i - 1] - 1;
for (int j = sz[i] - 1; j >= 0; j--) {
while (ptr >= 0 && v[i - 1][ptr] >= v[i][j]) {
mx = max(mx, dpdec[i - 1][ptr]);
ptr--;
}
dpdec[i][j] = mx - pref[i][j];
}
}
if (i > 2) {
// connect with i - 2
{
// i - 2 is greater
ll mx = 0;
for (int j = 0; j < sz[i - 2]; j++) mx = max(mx, dpdec[i - 2][j]);
int ptr = 0;
ll s = 0;
for (int j = 0; j < sz[i]; j++) {
while (ptr < sz[i - 1] && v[i - 1][ptr] <= v[i][j]) {
s = pref[i - 1][ptr];
ptr++;
}
dpinc[i][j] = max(dpinc[i][j], mx - s);
}
}
{
ll mx = 0;
int ptr = 0;
ll s = 0;
for (int j = 0; j < sz[i - 2]; j++) {
while (ptr < sz[i - 1] && v[i - 1][ptr] <= v[i - 2][j]) {
s = pref[i - 1][ptr];
ptr++;
}
mx = max(mx, dpdec[i - 2][j] - s);
}
ptr = 0;
s = 0;
for (int j = 0; j < sz[i]; j++) {
while (ptr < sz[i - 1] && v[i - 1][ptr] <= v[i][j]) {
s = pref[i - 1][ptr];
ptr++;
}
dpinc[i][j] = max(dpinc[i][j], mx + s);
}
}
}
{
// my inc -> my dec
for (int j = 0; j < sz[i]; j++) dpdec[i][j] = max(dpdec[i][j], dpinc[i][j]);
}
{
// add right to dpdec
int ptr = 0;
ll s = 0;
for (int j = 0; j < sz[i]; j++) {
while (ptr < sz[i + 1] && v[i + 1][ptr] <= v[i][j]) {
s = pref[i + 1][ptr];
ptr++;
}
dpdec[i][j] += s;
}
}
}
ll ans = 0;
for (int i = 1; i <= n; i++)
for (int j = 0; j < sz[i]; j++)
ans = max(ans, dpdec[i][j]);
return ans;
}
/*
5 4
0 2 5
1 1 2
4 4 1
3 3 3
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
88 ms |
54512 KB |
Output is correct |
2 |
Correct |
101 ms |
57660 KB |
Output is correct |
3 |
Correct |
50 ms |
48424 KB |
Output is correct |
4 |
Correct |
48 ms |
48340 KB |
Output is correct |
5 |
Correct |
245 ms |
83356 KB |
Output is correct |
6 |
Correct |
398 ms |
81380 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
35540 KB |
Output is correct |
2 |
Correct |
147 ms |
60436 KB |
Output is correct |
3 |
Correct |
179 ms |
64320 KB |
Output is correct |
4 |
Correct |
93 ms |
54576 KB |
Output is correct |
5 |
Correct |
107 ms |
57748 KB |
Output is correct |
6 |
Correct |
20 ms |
35540 KB |
Output is correct |
7 |
Correct |
18 ms |
35492 KB |
Output is correct |
8 |
Correct |
17 ms |
35488 KB |
Output is correct |
9 |
Correct |
18 ms |
35412 KB |
Output is correct |
10 |
Correct |
40 ms |
48332 KB |
Output is correct |
11 |
Correct |
43 ms |
48392 KB |
Output is correct |
12 |
Correct |
106 ms |
56668 KB |
Output is correct |
13 |
Correct |
111 ms |
59988 KB |
Output is correct |
14 |
Correct |
98 ms |
55728 KB |
Output is correct |
15 |
Correct |
106 ms |
55948 KB |
Output is correct |
16 |
Correct |
108 ms |
55788 KB |
Output is correct |
17 |
Correct |
118 ms |
57800 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
54 ms |
48364 KB |
Output is correct |
2 |
Correct |
42 ms |
48412 KB |
Output is correct |
3 |
Correct |
79 ms |
50280 KB |
Output is correct |
4 |
Correct |
68 ms |
50616 KB |
Output is correct |
5 |
Correct |
132 ms |
54116 KB |
Output is correct |
6 |
Correct |
109 ms |
54060 KB |
Output is correct |
7 |
Correct |
127 ms |
54140 KB |
Output is correct |
8 |
Correct |
123 ms |
54072 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
35520 KB |
Output is correct |
2 |
Correct |
19 ms |
35516 KB |
Output is correct |
3 |
Correct |
19 ms |
35540 KB |
Output is correct |
4 |
Correct |
19 ms |
35532 KB |
Output is correct |
5 |
Correct |
20 ms |
35508 KB |
Output is correct |
6 |
Correct |
20 ms |
35500 KB |
Output is correct |
7 |
Correct |
18 ms |
35432 KB |
Output is correct |
8 |
Correct |
20 ms |
35412 KB |
Output is correct |
9 |
Correct |
17 ms |
35540 KB |
Output is correct |
10 |
Correct |
18 ms |
35736 KB |
Output is correct |
11 |
Correct |
18 ms |
35540 KB |
Output is correct |
12 |
Correct |
21 ms |
35668 KB |
Output is correct |
13 |
Correct |
17 ms |
35540 KB |
Output is correct |
14 |
Correct |
19 ms |
35628 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
35520 KB |
Output is correct |
2 |
Correct |
19 ms |
35516 KB |
Output is correct |
3 |
Correct |
19 ms |
35540 KB |
Output is correct |
4 |
Correct |
19 ms |
35532 KB |
Output is correct |
5 |
Correct |
20 ms |
35508 KB |
Output is correct |
6 |
Correct |
20 ms |
35500 KB |
Output is correct |
7 |
Correct |
18 ms |
35432 KB |
Output is correct |
8 |
Correct |
20 ms |
35412 KB |
Output is correct |
9 |
Correct |
17 ms |
35540 KB |
Output is correct |
10 |
Correct |
18 ms |
35736 KB |
Output is correct |
11 |
Correct |
18 ms |
35540 KB |
Output is correct |
12 |
Correct |
21 ms |
35668 KB |
Output is correct |
13 |
Correct |
17 ms |
35540 KB |
Output is correct |
14 |
Correct |
19 ms |
35628 KB |
Output is correct |
15 |
Correct |
17 ms |
35540 KB |
Output is correct |
16 |
Correct |
17 ms |
35672 KB |
Output is correct |
17 |
Correct |
40 ms |
40008 KB |
Output is correct |
18 |
Correct |
46 ms |
40044 KB |
Output is correct |
19 |
Correct |
43 ms |
39896 KB |
Output is correct |
20 |
Correct |
40 ms |
39760 KB |
Output is correct |
21 |
Correct |
39 ms |
39612 KB |
Output is correct |
22 |
Correct |
64 ms |
43860 KB |
Output is correct |
23 |
Correct |
23 ms |
36692 KB |
Output is correct |
24 |
Correct |
39 ms |
39188 KB |
Output is correct |
25 |
Correct |
19 ms |
35704 KB |
Output is correct |
26 |
Correct |
33 ms |
36588 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
35520 KB |
Output is correct |
2 |
Correct |
19 ms |
35516 KB |
Output is correct |
3 |
Correct |
19 ms |
35540 KB |
Output is correct |
4 |
Correct |
19 ms |
35532 KB |
Output is correct |
5 |
Correct |
20 ms |
35508 KB |
Output is correct |
6 |
Correct |
20 ms |
35500 KB |
Output is correct |
7 |
Correct |
18 ms |
35432 KB |
Output is correct |
8 |
Correct |
20 ms |
35412 KB |
Output is correct |
9 |
Correct |
17 ms |
35540 KB |
Output is correct |
10 |
Correct |
18 ms |
35736 KB |
Output is correct |
11 |
Correct |
18 ms |
35540 KB |
Output is correct |
12 |
Correct |
21 ms |
35668 KB |
Output is correct |
13 |
Correct |
17 ms |
35540 KB |
Output is correct |
14 |
Correct |
19 ms |
35628 KB |
Output is correct |
15 |
Correct |
17 ms |
35540 KB |
Output is correct |
16 |
Correct |
17 ms |
35672 KB |
Output is correct |
17 |
Correct |
40 ms |
40008 KB |
Output is correct |
18 |
Correct |
46 ms |
40044 KB |
Output is correct |
19 |
Correct |
43 ms |
39896 KB |
Output is correct |
20 |
Correct |
40 ms |
39760 KB |
Output is correct |
21 |
Correct |
39 ms |
39612 KB |
Output is correct |
22 |
Correct |
64 ms |
43860 KB |
Output is correct |
23 |
Correct |
23 ms |
36692 KB |
Output is correct |
24 |
Correct |
39 ms |
39188 KB |
Output is correct |
25 |
Correct |
19 ms |
35704 KB |
Output is correct |
26 |
Correct |
33 ms |
36588 KB |
Output is correct |
27 |
Correct |
21 ms |
36152 KB |
Output is correct |
28 |
Correct |
128 ms |
56996 KB |
Output is correct |
29 |
Correct |
191 ms |
67956 KB |
Output is correct |
30 |
Correct |
210 ms |
78532 KB |
Output is correct |
31 |
Correct |
223 ms |
78340 KB |
Output is correct |
32 |
Correct |
186 ms |
64376 KB |
Output is correct |
33 |
Correct |
215 ms |
78960 KB |
Output is correct |
34 |
Correct |
232 ms |
79096 KB |
Output is correct |
35 |
Correct |
91 ms |
49932 KB |
Output is correct |
36 |
Correct |
218 ms |
74676 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
54 ms |
48364 KB |
Output is correct |
2 |
Correct |
42 ms |
48412 KB |
Output is correct |
3 |
Correct |
79 ms |
50280 KB |
Output is correct |
4 |
Correct |
68 ms |
50616 KB |
Output is correct |
5 |
Correct |
132 ms |
54116 KB |
Output is correct |
6 |
Correct |
109 ms |
54060 KB |
Output is correct |
7 |
Correct |
127 ms |
54140 KB |
Output is correct |
8 |
Correct |
123 ms |
54072 KB |
Output is correct |
9 |
Correct |
116 ms |
58952 KB |
Output is correct |
10 |
Correct |
87 ms |
46924 KB |
Output is correct |
11 |
Correct |
244 ms |
61400 KB |
Output is correct |
12 |
Correct |
19 ms |
35536 KB |
Output is correct |
13 |
Correct |
17 ms |
35528 KB |
Output is correct |
14 |
Correct |
18 ms |
35412 KB |
Output is correct |
15 |
Correct |
17 ms |
35412 KB |
Output is correct |
16 |
Correct |
18 ms |
35540 KB |
Output is correct |
17 |
Correct |
18 ms |
35540 KB |
Output is correct |
18 |
Correct |
42 ms |
48436 KB |
Output is correct |
19 |
Correct |
52 ms |
48396 KB |
Output is correct |
20 |
Correct |
50 ms |
48356 KB |
Output is correct |
21 |
Correct |
41 ms |
48440 KB |
Output is correct |
22 |
Correct |
175 ms |
59100 KB |
Output is correct |
23 |
Correct |
200 ms |
67288 KB |
Output is correct |
24 |
Correct |
222 ms |
68640 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
88 ms |
54512 KB |
Output is correct |
2 |
Correct |
101 ms |
57660 KB |
Output is correct |
3 |
Correct |
50 ms |
48424 KB |
Output is correct |
4 |
Correct |
48 ms |
48340 KB |
Output is correct |
5 |
Correct |
245 ms |
83356 KB |
Output is correct |
6 |
Correct |
398 ms |
81380 KB |
Output is correct |
7 |
Correct |
18 ms |
35540 KB |
Output is correct |
8 |
Correct |
147 ms |
60436 KB |
Output is correct |
9 |
Correct |
179 ms |
64320 KB |
Output is correct |
10 |
Correct |
93 ms |
54576 KB |
Output is correct |
11 |
Correct |
107 ms |
57748 KB |
Output is correct |
12 |
Correct |
20 ms |
35540 KB |
Output is correct |
13 |
Correct |
18 ms |
35492 KB |
Output is correct |
14 |
Correct |
17 ms |
35488 KB |
Output is correct |
15 |
Correct |
18 ms |
35412 KB |
Output is correct |
16 |
Correct |
40 ms |
48332 KB |
Output is correct |
17 |
Correct |
43 ms |
48392 KB |
Output is correct |
18 |
Correct |
106 ms |
56668 KB |
Output is correct |
19 |
Correct |
111 ms |
59988 KB |
Output is correct |
20 |
Correct |
98 ms |
55728 KB |
Output is correct |
21 |
Correct |
106 ms |
55948 KB |
Output is correct |
22 |
Correct |
108 ms |
55788 KB |
Output is correct |
23 |
Correct |
118 ms |
57800 KB |
Output is correct |
24 |
Correct |
54 ms |
48364 KB |
Output is correct |
25 |
Correct |
42 ms |
48412 KB |
Output is correct |
26 |
Correct |
79 ms |
50280 KB |
Output is correct |
27 |
Correct |
68 ms |
50616 KB |
Output is correct |
28 |
Correct |
132 ms |
54116 KB |
Output is correct |
29 |
Correct |
109 ms |
54060 KB |
Output is correct |
30 |
Correct |
127 ms |
54140 KB |
Output is correct |
31 |
Correct |
123 ms |
54072 KB |
Output is correct |
32 |
Correct |
18 ms |
35520 KB |
Output is correct |
33 |
Correct |
19 ms |
35516 KB |
Output is correct |
34 |
Correct |
19 ms |
35540 KB |
Output is correct |
35 |
Correct |
19 ms |
35532 KB |
Output is correct |
36 |
Correct |
20 ms |
35508 KB |
Output is correct |
37 |
Correct |
20 ms |
35500 KB |
Output is correct |
38 |
Correct |
18 ms |
35432 KB |
Output is correct |
39 |
Correct |
20 ms |
35412 KB |
Output is correct |
40 |
Correct |
17 ms |
35540 KB |
Output is correct |
41 |
Correct |
18 ms |
35736 KB |
Output is correct |
42 |
Correct |
18 ms |
35540 KB |
Output is correct |
43 |
Correct |
21 ms |
35668 KB |
Output is correct |
44 |
Correct |
17 ms |
35540 KB |
Output is correct |
45 |
Correct |
19 ms |
35628 KB |
Output is correct |
46 |
Correct |
17 ms |
35540 KB |
Output is correct |
47 |
Correct |
17 ms |
35672 KB |
Output is correct |
48 |
Correct |
40 ms |
40008 KB |
Output is correct |
49 |
Correct |
46 ms |
40044 KB |
Output is correct |
50 |
Correct |
43 ms |
39896 KB |
Output is correct |
51 |
Correct |
40 ms |
39760 KB |
Output is correct |
52 |
Correct |
39 ms |
39612 KB |
Output is correct |
53 |
Correct |
64 ms |
43860 KB |
Output is correct |
54 |
Correct |
23 ms |
36692 KB |
Output is correct |
55 |
Correct |
39 ms |
39188 KB |
Output is correct |
56 |
Correct |
19 ms |
35704 KB |
Output is correct |
57 |
Correct |
33 ms |
36588 KB |
Output is correct |
58 |
Correct |
21 ms |
36152 KB |
Output is correct |
59 |
Correct |
128 ms |
56996 KB |
Output is correct |
60 |
Correct |
191 ms |
67956 KB |
Output is correct |
61 |
Correct |
210 ms |
78532 KB |
Output is correct |
62 |
Correct |
223 ms |
78340 KB |
Output is correct |
63 |
Correct |
186 ms |
64376 KB |
Output is correct |
64 |
Correct |
215 ms |
78960 KB |
Output is correct |
65 |
Correct |
232 ms |
79096 KB |
Output is correct |
66 |
Correct |
91 ms |
49932 KB |
Output is correct |
67 |
Correct |
218 ms |
74676 KB |
Output is correct |
68 |
Correct |
116 ms |
58952 KB |
Output is correct |
69 |
Correct |
87 ms |
46924 KB |
Output is correct |
70 |
Correct |
244 ms |
61400 KB |
Output is correct |
71 |
Correct |
19 ms |
35536 KB |
Output is correct |
72 |
Correct |
17 ms |
35528 KB |
Output is correct |
73 |
Correct |
18 ms |
35412 KB |
Output is correct |
74 |
Correct |
17 ms |
35412 KB |
Output is correct |
75 |
Correct |
18 ms |
35540 KB |
Output is correct |
76 |
Correct |
18 ms |
35540 KB |
Output is correct |
77 |
Correct |
42 ms |
48436 KB |
Output is correct |
78 |
Correct |
52 ms |
48396 KB |
Output is correct |
79 |
Correct |
50 ms |
48356 KB |
Output is correct |
80 |
Correct |
41 ms |
48440 KB |
Output is correct |
81 |
Correct |
175 ms |
59100 KB |
Output is correct |
82 |
Correct |
200 ms |
67288 KB |
Output is correct |
83 |
Correct |
222 ms |
68640 KB |
Output is correct |
84 |
Correct |
269 ms |
85188 KB |
Output is correct |
85 |
Correct |
265 ms |
85836 KB |
Output is correct |
86 |
Correct |
368 ms |
87340 KB |
Output is correct |
87 |
Correct |
355 ms |
88436 KB |
Output is correct |
88 |
Correct |
18 ms |
35500 KB |
Output is correct |
89 |
Correct |
367 ms |
88428 KB |
Output is correct |
90 |
Correct |
342 ms |
83204 KB |
Output is correct |
91 |
Correct |
230 ms |
77316 KB |
Output is correct |