#include "fish.h"
#include <bits/stdc++.h>
using namespace std;
const long long inf = -(1LL << 50);
long long max_weights(int n, int m, vector<int> x, vector<int> y, vector<int> w) {
vector<vector<int>> b(n);
for (int i = 0; i < m; i++) {
b[x[i]].push_back(i);
}
vector d(n, vector<int>{0});
for (int i = 0; i < n; i++) {
for (int j = 1; j <= n; j++) {
d[i].push_back(j);
}
}
vector<vector<int>> r(n), p(n);
for (int i = 0; i < n; i++) {
sort(b[i].begin(), b[i].end(), [&](int j, int k) {
return y[j] < y[k];
});
for (int j : b[i]) {
r[i].push_back(y[j] + 1);
p[i].push_back(w[j]);
}
}
vector<vector<long long>> ssum(n), psum(n);
for (int i = 0; i < n; i++) {
int k = p[i].size();
psum[i] = vector<long long>(k + 1);
ssum[i] = vector<long long>(k + 1);
for (int j = 0; j < k; j++) {
psum[i][j + 1] = psum[i][j] + p[i][j];
}
for (int j = k - 1; j >= 0; j--) {
ssum[i][j] = ssum[i][j + 1] + p[i][j];
}
}
auto suf_sum = [&](int i, int x) -> long long {
if (i == -1) {
return 0;
}
return ssum[i][upper_bound(r[i].begin(), r[i].end(), x) - r[i].begin()];
};
auto pre_sum = [&](int i, int x) -> long long {
if (i == -1) {
return 0;
}
return psum[i][upper_bound(r[i].begin(), r[i].end(), x) - r[i].begin()];
};
vector<int> e{0};
vector<array<long long, 2>> dp{{0, 0}};
for (int i = 0; i < n; i++) {
vector<array<long long, 2>> pd;
pd.swap(dp);
int l = d[i].size();
dp = vector(l, array<long long, 2>{inf, inf});
int k = pd.size();
vector<long long> pmax(k + 1, inf);
for (int j = 0; j < k; j++) {
pmax[j + 1] = max(pmax[j], pd[j][0] + suf_sum(i - 1, e[j]));
dp[0][0] = max(dp[0][0], max(pd[j][0], pd[j][1]));
dp[0][1] = max(dp[0][1], max(pd[j][0], pd[j][1]) + pre_sum(i, e[j]));
}
vector<long long> smax(k + 1, inf);
for (int j = k - 1; j >= 0; j--) {
smax[j] = max(smax[j + 1], max(pd[j][0], pd[j][1]) - suf_sum(i, e[j]));
}
for (int j = 1; j < l; j++) {
dp[j][0] = max({dp[j][0], max(pd[0][1], pd[0][0] + pre_sum(i - 1, d[i][j])), pmax[upper_bound(e.begin(), e.end(), d[i][j]) - e.begin()] - suf_sum(i - 1, d[i][j])});
dp[j][1] = max({dp[j][1], max(pd[0][1], pd[0][0] + pre_sum(i - 1, d[i][j])), smax[lower_bound(e.begin(), e.end(), d[i][j]) - e.begin()] + suf_sum(i, d[i][j])});
}
e.swap(d[i]);
}
long long ans = inf;
for (auto& v : dp) {
ans = max(ans, max(v[0], v[1]));
}
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1120 ms |
685200 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Execution timed out |
1082 ms |
595028 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1104 ms |
673064 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
256 KB |
Output is correct |
2 |
Correct |
0 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 |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
2 ms |
468 KB |
Output is correct |
10 |
Correct |
9 ms |
980 KB |
Output is correct |
11 |
Correct |
3 ms |
468 KB |
Output is correct |
12 |
Correct |
9 ms |
980 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
10 ms |
980 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
256 KB |
Output is correct |
2 |
Correct |
0 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 |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
2 ms |
468 KB |
Output is correct |
10 |
Correct |
9 ms |
980 KB |
Output is correct |
11 |
Correct |
3 ms |
468 KB |
Output is correct |
12 |
Correct |
9 ms |
980 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
10 ms |
980 KB |
Output is correct |
15 |
Correct |
7 ms |
980 KB |
Output is correct |
16 |
Correct |
2 ms |
472 KB |
Output is correct |
17 |
Correct |
39 ms |
3276 KB |
Output is correct |
18 |
Correct |
35 ms |
3804 KB |
Output is correct |
19 |
Correct |
32 ms |
3616 KB |
Output is correct |
20 |
Correct |
28 ms |
3668 KB |
Output is correct |
21 |
Correct |
29 ms |
3656 KB |
Output is correct |
22 |
Correct |
58 ms |
6416 KB |
Output is correct |
23 |
Correct |
21 ms |
1364 KB |
Output is correct |
24 |
Correct |
34 ms |
2516 KB |
Output is correct |
25 |
Correct |
9 ms |
1060 KB |
Output is correct |
26 |
Correct |
18 ms |
1368 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
256 KB |
Output is correct |
2 |
Correct |
0 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 |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
2 ms |
468 KB |
Output is correct |
10 |
Correct |
9 ms |
980 KB |
Output is correct |
11 |
Correct |
3 ms |
468 KB |
Output is correct |
12 |
Correct |
9 ms |
980 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
10 ms |
980 KB |
Output is correct |
15 |
Correct |
7 ms |
980 KB |
Output is correct |
16 |
Correct |
2 ms |
472 KB |
Output is correct |
17 |
Correct |
39 ms |
3276 KB |
Output is correct |
18 |
Correct |
35 ms |
3804 KB |
Output is correct |
19 |
Correct |
32 ms |
3616 KB |
Output is correct |
20 |
Correct |
28 ms |
3668 KB |
Output is correct |
21 |
Correct |
29 ms |
3656 KB |
Output is correct |
22 |
Correct |
58 ms |
6416 KB |
Output is correct |
23 |
Correct |
21 ms |
1364 KB |
Output is correct |
24 |
Correct |
34 ms |
2516 KB |
Output is correct |
25 |
Correct |
9 ms |
1060 KB |
Output is correct |
26 |
Correct |
18 ms |
1368 KB |
Output is correct |
27 |
Correct |
938 ms |
48736 KB |
Output is correct |
28 |
Correct |
189 ms |
16392 KB |
Output is correct |
29 |
Execution timed out |
1093 ms |
65308 KB |
Time limit exceeded |
30 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1104 ms |
673064 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1120 ms |
685200 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |