#include <bits/stdc++.h>
//#include <fish.h>
using namespace std;
template<class T>
class SegmentTree {
public:
SegmentTree (int N) {
N = (1 << ((int)floor(log2(N - 1)) + 1));
this->N = N;
val.assign(2 * N, ID);
}
void update (int x, T y) {
x += N - 1;
val[x] = y;
while (x != 0) {
x = (x - 1)/2;
val[x] = merge(val[2 * x + 1], val[2 * x + 2]);
}
}
T query (int ind, const int l, const int r, int tl, int tr) {
if (tl >= l && tr <= r) {
return val[ind];
}
if (tr < l || tl > r) {
return ID;
}
return merge(query(2 * ind + 1, l, r, tl, (tl + tr)/2), query(2 * ind + 2, l, r, (tl + tr)/2 + 1, tr));
}
T query (int l, int r) {
return query(0, l, r, 0, N - 1);
}
private:
vector<T> val;
T ID = -1e17;
T merge (T x, T y) {
return max(x, y);
}
int N;
};
int64_t max_weights(int N, int M, vector<int> X, vector<int> Y, vector<int> W) {
for (int i = 0; i < X.size(); i++) {
X[i]++;
}
bool all_even = true;
for (int i: X) {
if (i % 2 == 0) {
all_even = false;
}
}
if (all_even) {
int64_t w= 0;
for (int o: W) {
w += o;
}
return w;
}
int my = 0;
for (int y: Y) {
my = max(my, y);
}
my ++;
vector<pair<int,int64_t> > vec[N + 10];
map<int,int64_t> s[N + 10];
for (int i = 0; i < X.size(); i++) {
vec[X[i]].push_back(make_pair(Y[i], W[i]));
s[X[i]][Y[i]] = W[i];
}
int64_t pref[N + 10][my + 2];
for (int i = 0; i <= N + 9; i++) {
pref[i][0] = 0;
for (int j = 1; j <= my + 1; j++) {
pref[i][j] = pref[i][j - 1] + s[i][j - 1];
}
}
int64_t dp[N + 1][my + 1][3];
for (int i = 0; i <= N; i++) {
for (int j = 0; j <= my; j++) {
for (int d = 0; d < 3; d++) {
dp[i][j][d] = -1e17;
}
}
}
dp[0][0][0] = 0;
//0 --> stagnant
//1 --> increasing
//2 --> decreasing
for (int i = 1; i <= N; i++) {
SegmentTree<int64_t> st1(my + 1);
for (int j = 0; j <= my; j++) {
st1.update(j, dp[i - 1][j][2]);
}
for (int p = 0; p <= my; p++) {
dp[i][0][0] = max(dp[i][0][0], dp[i - 1][p][0]);
}
int64_t myMax1 = -1e17;
int64_t myMax2 = dp[i - 1][0][0] - pref[i - 1][0];
for (int cur = 1; cur <= my; cur++) {
dp[i][cur][0] = max(dp[i][cur][0], max(dp[i - 1][cur][1], dp[i - 1][cur][2]));
myMax1 = max(myMax1, dp[i - 1][cur][1] - pref[i][cur] - pref[i - 1][cur]);
dp[i][cur][1] = max(dp[i][cur][1], myMax1 + pref[i + 1][cur] + pref[i - 1][cur]);
dp[i][cur][2] = max(dp[i][cur][2], st1.query(cur, my) - pref[i][cur] + pref[i + 1][cur]);
for (int d = 1; d <= 2; d++) {
myMax2 = max(myMax2, dp[i - 1][cur][0] - pref[i - 1][cur]);
dp[i][cur][d] = max(dp[i][cur][d], myMax2 + pref[i - 1][cur] + pref[i + 1][cur]);
}
}
int64_t myMax3 = -1e17;
for (int cur = my; cur >= 1; cur--) {
for (int d = 1; d <= 2; d++) {
myMax3 = max(myMax3, dp[i - 1][cur][0]);
dp[i][cur][d] = max(dp[i][cur][d], myMax3 + pref[i + 1][cur]);
}
}
}
int64_t myMax = 0;
for (int i = 0; i <= N; i++) {
for (int j = 0; j <= my; j++) {
for (int d = 0; d <= 2; d++) {
myMax = max(myMax, dp[i][j][d]);
}
}
}
return myMax;
}
Compilation message
fish.cpp: In function 'int64_t max_weights(int, int, std::vector<int>, std::vector<int>, std::vector<int>)':
fish.cpp:46:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
46 | for (int i = 0; i < X.size(); i++) {
| ~~^~~~~~~~~~
fish.cpp:69:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
69 | for (int i = 0; i < X.size(); i++) {
| ~~^~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
22 ms |
2132 KB |
Output is correct |
2 |
Correct |
26 ms |
2644 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
83 ms |
7284 KB |
Output is correct |
6 |
Correct |
85 ms |
7264 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Runtime error |
903 ms |
2097152 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
37 ms |
26900 KB |
Output is correct |
3 |
Correct |
72 ms |
27864 KB |
Output is correct |
4 |
Correct |
57 ms |
28972 KB |
Output is correct |
5 |
Correct |
110 ms |
32428 KB |
Output is correct |
6 |
Correct |
104 ms |
33024 KB |
Output is correct |
7 |
Correct |
112 ms |
33656 KB |
Output is correct |
8 |
Correct |
123 ms |
33632 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 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 |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
2 ms |
596 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
596 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 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 |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
2 ms |
596 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
596 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
23 ms |
8916 KB |
Output is correct |
16 |
Correct |
3 ms |
980 KB |
Output is correct |
17 |
Correct |
53 ms |
11520 KB |
Output is correct |
18 |
Correct |
62 ms |
11956 KB |
Output is correct |
19 |
Correct |
53 ms |
12032 KB |
Output is correct |
20 |
Correct |
54 ms |
11956 KB |
Output is correct |
21 |
Correct |
32 ms |
7496 KB |
Output is correct |
22 |
Correct |
76 ms |
14972 KB |
Output is correct |
23 |
Correct |
29 ms |
9376 KB |
Output is correct |
24 |
Correct |
41 ms |
10668 KB |
Output is correct |
25 |
Correct |
2 ms |
852 KB |
Output is correct |
26 |
Correct |
30 ms |
9364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 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 |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
2 ms |
596 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
596 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
23 ms |
8916 KB |
Output is correct |
16 |
Correct |
3 ms |
980 KB |
Output is correct |
17 |
Correct |
53 ms |
11520 KB |
Output is correct |
18 |
Correct |
62 ms |
11956 KB |
Output is correct |
19 |
Correct |
53 ms |
12032 KB |
Output is correct |
20 |
Correct |
54 ms |
11956 KB |
Output is correct |
21 |
Correct |
32 ms |
7496 KB |
Output is correct |
22 |
Correct |
76 ms |
14972 KB |
Output is correct |
23 |
Correct |
29 ms |
9376 KB |
Output is correct |
24 |
Correct |
41 ms |
10668 KB |
Output is correct |
25 |
Correct |
2 ms |
852 KB |
Output is correct |
26 |
Correct |
30 ms |
9364 KB |
Output is correct |
27 |
Execution timed out |
1133 ms |
848236 KB |
Time limit exceeded |
28 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
37 ms |
26900 KB |
Output is correct |
3 |
Correct |
72 ms |
27864 KB |
Output is correct |
4 |
Correct |
57 ms |
28972 KB |
Output is correct |
5 |
Correct |
110 ms |
32428 KB |
Output is correct |
6 |
Correct |
104 ms |
33024 KB |
Output is correct |
7 |
Correct |
112 ms |
33656 KB |
Output is correct |
8 |
Correct |
123 ms |
33632 KB |
Output is correct |
9 |
Runtime error |
769 ms |
2097152 KB |
Execution killed with signal 9 |
10 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
22 ms |
2132 KB |
Output is correct |
2 |
Correct |
26 ms |
2644 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
83 ms |
7284 KB |
Output is correct |
6 |
Correct |
85 ms |
7264 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Runtime error |
903 ms |
2097152 KB |
Execution killed with signal 9 |
9 |
Halted |
0 ms |
0 KB |
- |