#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 = 2;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());
// 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 |
27 ms |
27600 KB |
Output is correct |
2 |
Correct |
31 ms |
28708 KB |
Output is correct |
3 |
Correct |
13 ms |
23128 KB |
Output is correct |
4 |
Correct |
12 ms |
23388 KB |
Output is correct |
5 |
Correct |
95 ms |
34888 KB |
Output is correct |
6 |
Correct |
98 ms |
36360 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
20056 KB |
Output is correct |
2 |
Incorrect |
47 ms |
30228 KB |
1st lines differ - on the 1st token, expected: '40604614618209', found: '40479197388443' |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
23308 KB |
Output is correct |
2 |
Correct |
12 ms |
23384 KB |
Output is correct |
3 |
Correct |
23 ms |
26460 KB |
Output is correct |
4 |
Correct |
19 ms |
24224 KB |
Output is correct |
5 |
Correct |
33 ms |
27728 KB |
Output is correct |
6 |
Correct |
31 ms |
27740 KB |
Output is correct |
7 |
Correct |
34 ms |
27740 KB |
Output is correct |
8 |
Correct |
37 ms |
27732 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
20232 KB |
Output is correct |
2 |
Correct |
3 ms |
20060 KB |
Output is correct |
3 |
Correct |
3 ms |
20060 KB |
Output is correct |
4 |
Correct |
5 ms |
20060 KB |
Output is correct |
5 |
Correct |
4 ms |
20060 KB |
Output is correct |
6 |
Correct |
3 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 |
20060 KB |
Output is correct |
10 |
Correct |
5 ms |
20232 KB |
Output is correct |
11 |
Correct |
3 ms |
20060 KB |
Output is correct |
12 |
Correct |
4 ms |
20232 KB |
Output is correct |
13 |
Correct |
3 ms |
20056 KB |
Output is correct |
14 |
Incorrect |
4 ms |
20224 KB |
1st lines differ - on the 1st token, expected: '360901324355', found: '360406343483' |
15 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
20232 KB |
Output is correct |
2 |
Correct |
3 ms |
20060 KB |
Output is correct |
3 |
Correct |
3 ms |
20060 KB |
Output is correct |
4 |
Correct |
5 ms |
20060 KB |
Output is correct |
5 |
Correct |
4 ms |
20060 KB |
Output is correct |
6 |
Correct |
3 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 |
20060 KB |
Output is correct |
10 |
Correct |
5 ms |
20232 KB |
Output is correct |
11 |
Correct |
3 ms |
20060 KB |
Output is correct |
12 |
Correct |
4 ms |
20232 KB |
Output is correct |
13 |
Correct |
3 ms |
20056 KB |
Output is correct |
14 |
Incorrect |
4 ms |
20224 KB |
1st lines differ - on the 1st token, expected: '360901324355', found: '360406343483' |
15 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
20232 KB |
Output is correct |
2 |
Correct |
3 ms |
20060 KB |
Output is correct |
3 |
Correct |
3 ms |
20060 KB |
Output is correct |
4 |
Correct |
5 ms |
20060 KB |
Output is correct |
5 |
Correct |
4 ms |
20060 KB |
Output is correct |
6 |
Correct |
3 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 |
20060 KB |
Output is correct |
10 |
Correct |
5 ms |
20232 KB |
Output is correct |
11 |
Correct |
3 ms |
20060 KB |
Output is correct |
12 |
Correct |
4 ms |
20232 KB |
Output is correct |
13 |
Correct |
3 ms |
20056 KB |
Output is correct |
14 |
Incorrect |
4 ms |
20224 KB |
1st lines differ - on the 1st token, expected: '360901324355', found: '360406343483' |
15 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
23308 KB |
Output is correct |
2 |
Correct |
12 ms |
23384 KB |
Output is correct |
3 |
Correct |
23 ms |
26460 KB |
Output is correct |
4 |
Correct |
19 ms |
24224 KB |
Output is correct |
5 |
Correct |
33 ms |
27728 KB |
Output is correct |
6 |
Correct |
31 ms |
27740 KB |
Output is correct |
7 |
Correct |
34 ms |
27740 KB |
Output is correct |
8 |
Correct |
37 ms |
27732 KB |
Output is correct |
9 |
Correct |
42 ms |
30184 KB |
Output is correct |
10 |
Correct |
33 ms |
27472 KB |
Output is correct |
11 |
Correct |
64 ms |
32596 KB |
Output is correct |
12 |
Correct |
4 ms |
20056 KB |
Output is correct |
13 |
Correct |
3 ms |
20252 KB |
Output is correct |
14 |
Correct |
3 ms |
20056 KB |
Output is correct |
15 |
Correct |
3 ms |
20060 KB |
Output is correct |
16 |
Correct |
4 ms |
20060 KB |
Output is correct |
17 |
Correct |
4 ms |
20060 KB |
Output is correct |
18 |
Correct |
12 ms |
23380 KB |
Output is correct |
19 |
Correct |
12 ms |
23132 KB |
Output is correct |
20 |
Correct |
13 ms |
23132 KB |
Output is correct |
21 |
Correct |
13 ms |
23388 KB |
Output is correct |
22 |
Correct |
44 ms |
29420 KB |
Output is correct |
23 |
Correct |
63 ms |
32588 KB |
Output is correct |
24 |
Correct |
70 ms |
32592 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
27 ms |
27600 KB |
Output is correct |
2 |
Correct |
31 ms |
28708 KB |
Output is correct |
3 |
Correct |
13 ms |
23128 KB |
Output is correct |
4 |
Correct |
12 ms |
23388 KB |
Output is correct |
5 |
Correct |
95 ms |
34888 KB |
Output is correct |
6 |
Correct |
98 ms |
36360 KB |
Output is correct |
7 |
Correct |
4 ms |
20056 KB |
Output is correct |
8 |
Incorrect |
47 ms |
30228 KB |
1st lines differ - on the 1st token, expected: '40604614618209', found: '40479197388443' |
9 |
Halted |
0 ms |
0 KB |
- |