#include "bits/extc++.h"
using namespace std;
template <typename T>
void dbgh(const T& t) {
cerr << t << endl;
}
template <typename T, typename... U>
void dbgh(const T& t, const U&... u) {
cerr << t << " | ";
dbgh(u...);
}
#ifdef DEBUG
#define dbg(...) \
cerr << "L" << __LINE__ << " [" << #__VA_ARGS__ << "]" \
<< ": "; \
dbgh(__VA_ARGS__)
#else
#define cerr \
if (false) \
cerr
#define dbg(...)
#endif
using ll = long long;
#define endl "\n"
#define long int64_t
#define sz(x) int(size(x))
ll max_weights(int n, vector<array<int, 3>> fish) {
vector<pair<int, long>> arr[n];
for (auto& [x, y, w] : fish) {
arr[x].emplace_back(y, w);
}
for (auto& a : arr) {
sort(begin(a), end(a));
long psum = 0;
for (auto& [_, x] : a) {
psum += x;
x = psum;
}
}
auto ibs = [&](int i) -> bool {
return 0 <= i && i < n;
};
auto psum = [&](int i, int x) -> long {
if (!ibs(i)) {
return 0;
}
auto it =
lower_bound(begin(arr[i]), end(arr[i]), pair<int, long> {x, 0});
if (it == arr[i].begin()) {
return 0;
}
return (--it)->second;
};
auto sum = [&](int i, int l, int r) -> long {
return max(long(0), psum(i, r) - psum(i, l));
};
auto get = [&](int i, int j) -> int {
if (!ibs(i)) {
return 0;
}
if (j < sz(arr[i])) {
return arr[i][j].first;
} else {
return n;
}
};
vector<long> dp[n + 1][2];
dp[n][0] = dp[n][1] = {0};
for (int i = n - 1; i >= 0; i--) {
int m = sz(arr[i]);
{
dp[i][0].resize(m + 1);
for (int j = 0; j <= m; j++) {
for (int k = 0; k < sz(dp[i + 1][0]); k++) {
dp[i][0][j] =
max({dp[i][0][j], dp[i + 1][1][k],
dp[i + 1][0][k] +
sum(i + 1, get(i + 1, k), get(i, j))});
}
}
}
{
dp[i][1].resize(m + 1);
for (int j = 0; j <= m; j++) {
for (int k = 0; k < sz(dp[i + 1][0]); k++) {
long x = max(
dp[i + 1][1][k],
dp[i + 1][0][k] + sum(i + 1, get(i + 1, k), get(i, j)));
dp[i][1][j] =
max(dp[i][1][j], x + sum(i, get(i, j), get(i + 1, k)));
}
}
}
}
return *max_element(begin(dp[0][1]), end(dp[0][1]));
}
ll max_weights(int n, int m, vector<int> xs, vector<int> ys, vector<int> ws) {
vector<array<int, 3>> arr;
for (int i = 0; i < m; i++) {
arr.push_back({xs[i], ys[i], ws[i]});
}
return max_weights(n, arr);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
70 ms |
19656 KB |
Output is correct |
2 |
Correct |
70 ms |
22840 KB |
Output is correct |
3 |
Correct |
18 ms |
13568 KB |
Output is correct |
4 |
Correct |
18 ms |
13504 KB |
Output is correct |
5 |
Correct |
210 ms |
43676 KB |
Output is correct |
6 |
Correct |
224 ms |
44720 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Execution timed out |
1063 ms |
26688 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
13524 KB |
Output is correct |
2 |
Correct |
22 ms |
13552 KB |
Output is correct |
3 |
Correct |
55 ms |
17100 KB |
Output is correct |
4 |
Correct |
38 ms |
16472 KB |
Output is correct |
5 |
Correct |
79 ms |
21396 KB |
Output is correct |
6 |
Correct |
67 ms |
22076 KB |
Output is correct |
7 |
Correct |
74 ms |
22200 KB |
Output is correct |
8 |
Correct |
73 ms |
22080 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 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 |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
3 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 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 |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
3 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
21 ms |
468 KB |
Output is correct |
17 |
Execution timed out |
1081 ms |
4560 KB |
Time limit exceeded |
18 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 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 |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
3 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
21 ms |
468 KB |
Output is correct |
17 |
Execution timed out |
1081 ms |
4560 KB |
Time limit exceeded |
18 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
13524 KB |
Output is correct |
2 |
Correct |
22 ms |
13552 KB |
Output is correct |
3 |
Correct |
55 ms |
17100 KB |
Output is correct |
4 |
Correct |
38 ms |
16472 KB |
Output is correct |
5 |
Correct |
79 ms |
21396 KB |
Output is correct |
6 |
Correct |
67 ms |
22076 KB |
Output is correct |
7 |
Correct |
74 ms |
22200 KB |
Output is correct |
8 |
Correct |
73 ms |
22080 KB |
Output is correct |
9 |
Correct |
90 ms |
22100 KB |
Output is correct |
10 |
Correct |
64 ms |
15100 KB |
Output is correct |
11 |
Correct |
155 ms |
30476 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
19 ms |
13704 KB |
Output is correct |
19 |
Correct |
18 ms |
13484 KB |
Output is correct |
20 |
Correct |
18 ms |
13524 KB |
Output is correct |
21 |
Correct |
18 ms |
13612 KB |
Output is correct |
22 |
Correct |
84 ms |
22348 KB |
Output is correct |
23 |
Correct |
160 ms |
28836 KB |
Output is correct |
24 |
Correct |
155 ms |
29844 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
70 ms |
19656 KB |
Output is correct |
2 |
Correct |
70 ms |
22840 KB |
Output is correct |
3 |
Correct |
18 ms |
13568 KB |
Output is correct |
4 |
Correct |
18 ms |
13504 KB |
Output is correct |
5 |
Correct |
210 ms |
43676 KB |
Output is correct |
6 |
Correct |
224 ms |
44720 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Execution timed out |
1063 ms |
26688 KB |
Time limit exceeded |
9 |
Halted |
0 ms |
0 KB |
- |