#include "fish.h"
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
vector<pair<int, int>> pos[100100];
struct DP {
ll in, de;
DP() {
in = de = 0;
}
} dp[1001000];
bool ___[100100];
int w[1001000];
int n, m;
ll max_weights(int N, int M, vector<int> X, vector<int> Y, vector<int> W) {
n = N, m = M;
for (int i = 0;i < m;i++) {
w[i + 1] = W[i];
pos[X[i] + 1].push_back({ Y[i] + 1, i + 1 });
if (Y[i] + 1 == 1) ___[X[i] + 1] = 1;
}
int idx = m, p;
ll mx, sum;
for (int i = 1;i <= n;i++) {
if (!___[i]) pos[i].push_back({ 1, ++idx });
pos[i].push_back({ n + 1, ++idx });
sort(pos[i].begin(), pos[i].end());
if (i == 1) continue;
// h[i-1] < h[i]
{
p = 0;
mx = 0;
for (int j = 0;j < (int)pos[i].size();j++) {
while (p < (int)pos[i - 1].size() && pos[i - 1][p].first <= pos[i][j].first - 1) {
int prev = pos[i - 1][p++].second;
mx = max(mx, dp[prev].in) + w[prev];
}
dp[pos[i][j].second].in = mx;
}
}
// h[i-1] >= h[i]
{
p = pos[i - 1].size() - 1;
mx = 0;
for (int j = pos[i].size() - 1;j >= 0;j--) {
while (p >= 0 && pos[i - 1][p].first > pos[i][j].first) {
int prev = pos[i - 1][p--].second;
mx = max({ mx, dp[prev].in, dp[prev].de });
}
mx += w[pos[i][j].second];
while (p >= 0 && pos[i - 1][p].first == pos[i][j].first) {
int prev = pos[i - 1][p--].second;
mx = max({ mx, dp[prev].in, dp[prev].de });
}
dp[pos[i][j].second].de = mx;
}
}
// in -> de, de -> in
if (i >= 3) {
// h[i-2] <= h[i]
{
int mid = 0;
p = 0;
mx = 0, sum = 0;
for (int j = 0;j < (int)pos[i].size();j++) {
while (p < (int)pos[i - 2].size() && pos[i - 2][p].first <= pos[i][j].first) {
int prev = pos[i - 2][p++].second;
mx = max({ mx ,dp[prev].in, dp[prev].de });
}
while (mid < (int)pos[i - 1].size() && pos[i - 1][mid].first <= pos[i][j].first - 1) {
sum += w[pos[i - 1][mid++].second];
}
dp[pos[i][j].second].in = max(dp[pos[i][j].second].in, mx + sum);
}
}
// h[i-2] >= h[i]
{
int mid = pos[i - 1].size() - 1;
mx = 0, sum = 0;
for (int j = 0;j <= mid;j++) sum += w[pos[i - 1][j].second];
p = pos[i - 2].size() - 1;
for (int j = pos[i].size() - 1;j >= 0;j--) {
while (p >= 0 && pos[i - 2][p].first >= pos[i][j].first) {
int prev = pos[i - 2][p].second;
int now = pos[i - 2][p].first - 1;
while (mid >= 0 && pos[i - 1][mid].first > now) {
sum -= w[pos[i - 1][mid--].second];
}
mx = max({ mx, dp[prev].in + sum, dp[prev].de + sum });
p--;
}
dp[pos[i][j].second].in = max(dp[pos[i][j].second].in, mx);
}
}
}
}
ll ans = 0;
for (int i = 1;i <= idx;i++) {
ans = max(ans, dp[i].in);
ans = max(ans, dp[i].de);
}
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
32 ms |
27592 KB |
Output is correct |
2 |
Correct |
42 ms |
28616 KB |
Output is correct |
3 |
Correct |
13 ms |
23384 KB |
Output is correct |
4 |
Correct |
15 ms |
23132 KB |
Output is correct |
5 |
Correct |
99 ms |
34880 KB |
Output is correct |
6 |
Correct |
100 ms |
36176 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
20060 KB |
Output is correct |
2 |
Correct |
57 ms |
30252 KB |
Output is correct |
3 |
Correct |
73 ms |
35128 KB |
Output is correct |
4 |
Correct |
33 ms |
29124 KB |
Output is correct |
5 |
Correct |
41 ms |
30384 KB |
Output is correct |
6 |
Correct |
4 ms |
20060 KB |
Output is correct |
7 |
Correct |
3 ms |
20060 KB |
Output is correct |
8 |
Correct |
3 ms |
20060 KB |
Output is correct |
9 |
Correct |
3 ms |
20256 KB |
Output is correct |
10 |
Correct |
13 ms |
23388 KB |
Output is correct |
11 |
Correct |
12 ms |
23388 KB |
Output is correct |
12 |
Correct |
33 ms |
29040 KB |
Output is correct |
13 |
Correct |
41 ms |
30312 KB |
Output is correct |
14 |
Correct |
35 ms |
29020 KB |
Output is correct |
15 |
Correct |
37 ms |
29896 KB |
Output is correct |
16 |
Correct |
42 ms |
29124 KB |
Output is correct |
17 |
Correct |
39 ms |
29892 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
23132 KB |
Output is correct |
2 |
Correct |
12 ms |
23388 KB |
Output is correct |
3 |
Correct |
22 ms |
26492 KB |
Output is correct |
4 |
Correct |
20 ms |
24156 KB |
Output is correct |
5 |
Correct |
35 ms |
27964 KB |
Output is correct |
6 |
Correct |
32 ms |
27740 KB |
Output is correct |
7 |
Correct |
38 ms |
27728 KB |
Output is correct |
8 |
Correct |
32 ms |
27728 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
20056 KB |
Output is correct |
2 |
Correct |
3 ms |
20060 KB |
Output is correct |
3 |
Correct |
4 ms |
20060 KB |
Output is correct |
4 |
Correct |
5 ms |
20060 KB |
Output is correct |
5 |
Correct |
3 ms |
20144 KB |
Output is correct |
6 |
Correct |
3 ms |
20060 KB |
Output is correct |
7 |
Correct |
3 ms |
20212 KB |
Output is correct |
8 |
Correct |
3 ms |
20056 KB |
Output is correct |
9 |
Correct |
3 ms |
20060 KB |
Output is correct |
10 |
Correct |
4 ms |
20316 KB |
Output is correct |
11 |
Correct |
4 ms |
20060 KB |
Output is correct |
12 |
Correct |
4 ms |
20056 KB |
Output is correct |
13 |
Correct |
3 ms |
20060 KB |
Output is correct |
14 |
Correct |
4 ms |
20240 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
20056 KB |
Output is correct |
2 |
Correct |
3 ms |
20060 KB |
Output is correct |
3 |
Correct |
4 ms |
20060 KB |
Output is correct |
4 |
Correct |
5 ms |
20060 KB |
Output is correct |
5 |
Correct |
3 ms |
20144 KB |
Output is correct |
6 |
Correct |
3 ms |
20060 KB |
Output is correct |
7 |
Correct |
3 ms |
20212 KB |
Output is correct |
8 |
Correct |
3 ms |
20056 KB |
Output is correct |
9 |
Correct |
3 ms |
20060 KB |
Output is correct |
10 |
Correct |
4 ms |
20316 KB |
Output is correct |
11 |
Correct |
4 ms |
20060 KB |
Output is correct |
12 |
Correct |
4 ms |
20056 KB |
Output is correct |
13 |
Correct |
3 ms |
20060 KB |
Output is correct |
14 |
Correct |
4 ms |
20240 KB |
Output is correct |
15 |
Correct |
6 ms |
20060 KB |
Output is correct |
16 |
Correct |
4 ms |
20316 KB |
Output is correct |
17 |
Correct |
19 ms |
24732 KB |
Output is correct |
18 |
Correct |
17 ms |
24932 KB |
Output is correct |
19 |
Correct |
16 ms |
24924 KB |
Output is correct |
20 |
Correct |
26 ms |
25004 KB |
Output is correct |
21 |
Correct |
26 ms |
24920 KB |
Output is correct |
22 |
Correct |
33 ms |
27316 KB |
Output is correct |
23 |
Correct |
7 ms |
20568 KB |
Output is correct |
24 |
Correct |
21 ms |
21852 KB |
Output is correct |
25 |
Correct |
4 ms |
20316 KB |
Output is correct |
26 |
Correct |
5 ms |
20572 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
20056 KB |
Output is correct |
2 |
Correct |
3 ms |
20060 KB |
Output is correct |
3 |
Correct |
4 ms |
20060 KB |
Output is correct |
4 |
Correct |
5 ms |
20060 KB |
Output is correct |
5 |
Correct |
3 ms |
20144 KB |
Output is correct |
6 |
Correct |
3 ms |
20060 KB |
Output is correct |
7 |
Correct |
3 ms |
20212 KB |
Output is correct |
8 |
Correct |
3 ms |
20056 KB |
Output is correct |
9 |
Correct |
3 ms |
20060 KB |
Output is correct |
10 |
Correct |
4 ms |
20316 KB |
Output is correct |
11 |
Correct |
4 ms |
20060 KB |
Output is correct |
12 |
Correct |
4 ms |
20056 KB |
Output is correct |
13 |
Correct |
3 ms |
20060 KB |
Output is correct |
14 |
Correct |
4 ms |
20240 KB |
Output is correct |
15 |
Correct |
6 ms |
20060 KB |
Output is correct |
16 |
Correct |
4 ms |
20316 KB |
Output is correct |
17 |
Correct |
19 ms |
24732 KB |
Output is correct |
18 |
Correct |
17 ms |
24932 KB |
Output is correct |
19 |
Correct |
16 ms |
24924 KB |
Output is correct |
20 |
Correct |
26 ms |
25004 KB |
Output is correct |
21 |
Correct |
26 ms |
24920 KB |
Output is correct |
22 |
Correct |
33 ms |
27316 KB |
Output is correct |
23 |
Correct |
7 ms |
20568 KB |
Output is correct |
24 |
Correct |
21 ms |
21852 KB |
Output is correct |
25 |
Correct |
4 ms |
20316 KB |
Output is correct |
26 |
Correct |
5 ms |
20572 KB |
Output is correct |
27 |
Correct |
5 ms |
20572 KB |
Output is correct |
28 |
Correct |
60 ms |
34424 KB |
Output is correct |
29 |
Correct |
89 ms |
38740 KB |
Output is correct |
30 |
Correct |
91 ms |
38464 KB |
Output is correct |
31 |
Correct |
120 ms |
38344 KB |
Output is correct |
32 |
Correct |
75 ms |
39504 KB |
Output is correct |
33 |
Correct |
120 ms |
38480 KB |
Output is correct |
34 |
Correct |
91 ms |
38348 KB |
Output is correct |
35 |
Correct |
35 ms |
28656 KB |
Output is correct |
36 |
Correct |
88 ms |
39244 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
23132 KB |
Output is correct |
2 |
Correct |
12 ms |
23388 KB |
Output is correct |
3 |
Correct |
22 ms |
26492 KB |
Output is correct |
4 |
Correct |
20 ms |
24156 KB |
Output is correct |
5 |
Correct |
35 ms |
27964 KB |
Output is correct |
6 |
Correct |
32 ms |
27740 KB |
Output is correct |
7 |
Correct |
38 ms |
27728 KB |
Output is correct |
8 |
Correct |
32 ms |
27728 KB |
Output is correct |
9 |
Correct |
43 ms |
30300 KB |
Output is correct |
10 |
Correct |
32 ms |
27472 KB |
Output is correct |
11 |
Correct |
70 ms |
32588 KB |
Output is correct |
12 |
Correct |
3 ms |
20060 KB |
Output is correct |
13 |
Correct |
3 ms |
20236 KB |
Output is correct |
14 |
Correct |
3 ms |
20312 KB |
Output is correct |
15 |
Correct |
4 ms |
20060 KB |
Output is correct |
16 |
Correct |
3 ms |
20060 KB |
Output is correct |
17 |
Correct |
3 ms |
20060 KB |
Output is correct |
18 |
Correct |
22 ms |
23392 KB |
Output is correct |
19 |
Correct |
12 ms |
23388 KB |
Output is correct |
20 |
Correct |
13 ms |
23224 KB |
Output is correct |
21 |
Correct |
12 ms |
23132 KB |
Output is correct |
22 |
Correct |
45 ms |
29468 KB |
Output is correct |
23 |
Correct |
69 ms |
32592 KB |
Output is correct |
24 |
Correct |
70 ms |
32592 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
32 ms |
27592 KB |
Output is correct |
2 |
Correct |
42 ms |
28616 KB |
Output is correct |
3 |
Correct |
13 ms |
23384 KB |
Output is correct |
4 |
Correct |
15 ms |
23132 KB |
Output is correct |
5 |
Correct |
99 ms |
34880 KB |
Output is correct |
6 |
Correct |
100 ms |
36176 KB |
Output is correct |
7 |
Correct |
3 ms |
20060 KB |
Output is correct |
8 |
Correct |
57 ms |
30252 KB |
Output is correct |
9 |
Correct |
73 ms |
35128 KB |
Output is correct |
10 |
Correct |
33 ms |
29124 KB |
Output is correct |
11 |
Correct |
41 ms |
30384 KB |
Output is correct |
12 |
Correct |
4 ms |
20060 KB |
Output is correct |
13 |
Correct |
3 ms |
20060 KB |
Output is correct |
14 |
Correct |
3 ms |
20060 KB |
Output is correct |
15 |
Correct |
3 ms |
20256 KB |
Output is correct |
16 |
Correct |
13 ms |
23388 KB |
Output is correct |
17 |
Correct |
12 ms |
23388 KB |
Output is correct |
18 |
Correct |
33 ms |
29040 KB |
Output is correct |
19 |
Correct |
41 ms |
30312 KB |
Output is correct |
20 |
Correct |
35 ms |
29020 KB |
Output is correct |
21 |
Correct |
37 ms |
29896 KB |
Output is correct |
22 |
Correct |
42 ms |
29124 KB |
Output is correct |
23 |
Correct |
39 ms |
29892 KB |
Output is correct |
24 |
Correct |
13 ms |
23132 KB |
Output is correct |
25 |
Correct |
12 ms |
23388 KB |
Output is correct |
26 |
Correct |
22 ms |
26492 KB |
Output is correct |
27 |
Correct |
20 ms |
24156 KB |
Output is correct |
28 |
Correct |
35 ms |
27964 KB |
Output is correct |
29 |
Correct |
32 ms |
27740 KB |
Output is correct |
30 |
Correct |
38 ms |
27728 KB |
Output is correct |
31 |
Correct |
32 ms |
27728 KB |
Output is correct |
32 |
Correct |
4 ms |
20056 KB |
Output is correct |
33 |
Correct |
3 ms |
20060 KB |
Output is correct |
34 |
Correct |
4 ms |
20060 KB |
Output is correct |
35 |
Correct |
5 ms |
20060 KB |
Output is correct |
36 |
Correct |
3 ms |
20144 KB |
Output is correct |
37 |
Correct |
3 ms |
20060 KB |
Output is correct |
38 |
Correct |
3 ms |
20212 KB |
Output is correct |
39 |
Correct |
3 ms |
20056 KB |
Output is correct |
40 |
Correct |
3 ms |
20060 KB |
Output is correct |
41 |
Correct |
4 ms |
20316 KB |
Output is correct |
42 |
Correct |
4 ms |
20060 KB |
Output is correct |
43 |
Correct |
4 ms |
20056 KB |
Output is correct |
44 |
Correct |
3 ms |
20060 KB |
Output is correct |
45 |
Correct |
4 ms |
20240 KB |
Output is correct |
46 |
Correct |
6 ms |
20060 KB |
Output is correct |
47 |
Correct |
4 ms |
20316 KB |
Output is correct |
48 |
Correct |
19 ms |
24732 KB |
Output is correct |
49 |
Correct |
17 ms |
24932 KB |
Output is correct |
50 |
Correct |
16 ms |
24924 KB |
Output is correct |
51 |
Correct |
26 ms |
25004 KB |
Output is correct |
52 |
Correct |
26 ms |
24920 KB |
Output is correct |
53 |
Correct |
33 ms |
27316 KB |
Output is correct |
54 |
Correct |
7 ms |
20568 KB |
Output is correct |
55 |
Correct |
21 ms |
21852 KB |
Output is correct |
56 |
Correct |
4 ms |
20316 KB |
Output is correct |
57 |
Correct |
5 ms |
20572 KB |
Output is correct |
58 |
Correct |
5 ms |
20572 KB |
Output is correct |
59 |
Correct |
60 ms |
34424 KB |
Output is correct |
60 |
Correct |
89 ms |
38740 KB |
Output is correct |
61 |
Correct |
91 ms |
38464 KB |
Output is correct |
62 |
Correct |
120 ms |
38344 KB |
Output is correct |
63 |
Correct |
75 ms |
39504 KB |
Output is correct |
64 |
Correct |
120 ms |
38480 KB |
Output is correct |
65 |
Correct |
91 ms |
38348 KB |
Output is correct |
66 |
Correct |
35 ms |
28656 KB |
Output is correct |
67 |
Correct |
88 ms |
39244 KB |
Output is correct |
68 |
Correct |
43 ms |
30300 KB |
Output is correct |
69 |
Correct |
32 ms |
27472 KB |
Output is correct |
70 |
Correct |
70 ms |
32588 KB |
Output is correct |
71 |
Correct |
3 ms |
20060 KB |
Output is correct |
72 |
Correct |
3 ms |
20236 KB |
Output is correct |
73 |
Correct |
3 ms |
20312 KB |
Output is correct |
74 |
Correct |
4 ms |
20060 KB |
Output is correct |
75 |
Correct |
3 ms |
20060 KB |
Output is correct |
76 |
Correct |
3 ms |
20060 KB |
Output is correct |
77 |
Correct |
22 ms |
23392 KB |
Output is correct |
78 |
Correct |
12 ms |
23388 KB |
Output is correct |
79 |
Correct |
13 ms |
23224 KB |
Output is correct |
80 |
Correct |
12 ms |
23132 KB |
Output is correct |
81 |
Correct |
45 ms |
29468 KB |
Output is correct |
82 |
Correct |
69 ms |
32592 KB |
Output is correct |
83 |
Correct |
70 ms |
32592 KB |
Output is correct |
84 |
Correct |
132 ms |
41012 KB |
Output is correct |
85 |
Correct |
102 ms |
40736 KB |
Output is correct |
86 |
Correct |
112 ms |
42680 KB |
Output is correct |
87 |
Correct |
107 ms |
42832 KB |
Output is correct |
88 |
Correct |
4 ms |
20060 KB |
Output is correct |
89 |
Correct |
130 ms |
42828 KB |
Output is correct |
90 |
Correct |
102 ms |
41164 KB |
Output is correct |
91 |
Correct |
98 ms |
41160 KB |
Output is correct |