#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[600600];
int w[600600];
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 });
}
int idx = m, p;
ll mx, sum;
for (int i = 2;i <= n;i++) {
pos[i].push_back({ 0, ++idx });
pos[i].push_back({ n + 1, ++idx });
sort(pos[i].begin(), pos[i].end());
// in -> in
{
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) {
int prev = pos[i - 1][p++].second;
mx = max(mx + w[prev], dp[prev].in);
}
dp[pos[i][j].second].in = mx;
}
}
// de -> de , in -> de
{
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];
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 - 1) {
int prev = pos[i - 2][p].second;
mx = max({ mx ,dp[prev].in, dp[prev].de });
p++;
}
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 - 1) {
int prev = pos[i - 2][p].second;
int now = pos[i - 2][p].first;
mx = max({ mx, dp[prev].in + sum, dp[prev].de + sum });
while (mid >= 0 && pos[i - 1][mid].first > now - 1) {
sum -= w[pos[i - 1][mid--].second];
}
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;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
30 ms |
19172 KB |
Output is correct |
2 |
Correct |
33 ms |
20432 KB |
Output is correct |
3 |
Correct |
13 ms |
15448 KB |
Output is correct |
4 |
Correct |
13 ms |
15192 KB |
Output is correct |
5 |
Correct |
101 ms |
32184 KB |
Output is correct |
6 |
Correct |
112 ms |
33876 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
6 ms |
12124 KB |
1st lines differ - on the 1st token, expected: '2', found: '1' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
15196 KB |
Output is correct |
2 |
Correct |
13 ms |
15272 KB |
Output is correct |
3 |
Incorrect |
31 ms |
18512 KB |
1st lines differ - on the 1st token, expected: '21261825233649', found: '26722445760742' |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
5 ms |
12120 KB |
1st lines differ - on the 1st token, expected: '3', found: '2' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
5 ms |
12120 KB |
1st lines differ - on the 1st token, expected: '3', found: '2' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
5 ms |
12120 KB |
1st lines differ - on the 1st token, expected: '3', found: '2' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
15196 KB |
Output is correct |
2 |
Correct |
13 ms |
15272 KB |
Output is correct |
3 |
Incorrect |
31 ms |
18512 KB |
1st lines differ - on the 1st token, expected: '21261825233649', found: '26722445760742' |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
30 ms |
19172 KB |
Output is correct |
2 |
Correct |
33 ms |
20432 KB |
Output is correct |
3 |
Correct |
13 ms |
15448 KB |
Output is correct |
4 |
Correct |
13 ms |
15192 KB |
Output is correct |
5 |
Correct |
101 ms |
32184 KB |
Output is correct |
6 |
Correct |
112 ms |
33876 KB |
Output is correct |
7 |
Incorrect |
6 ms |
12124 KB |
1st lines differ - on the 1st token, expected: '2', found: '1' |
8 |
Halted |
0 ms |
0 KB |
- |