#include "fish.h"
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<int, pii> piii;
typedef pair<ll, ll> pll;
typedef pair<ll, pll> plll;
#define fi first
#define se second
const int INF = 1e9+1;
const int P = 1000000007;
const ll LLINF = (ll)1e18+1;
template <typename T>
ostream& operator<<(ostream& os, const vector<T>& v) { for(auto i : v) os << i << " "; os << "\n"; return os; }
template <typename T1, typename T2>
ostream& operator<<(ostream& os, const pair<T1, T2>& p) { os << p.fi << " " << p.se; return os; }
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef vector<vvll> vvvll;
ll pre(int x, int s, int e, vvll &B) {
if(x < 0 || x >= B.size()) return 0LL;
return B[x][e] - B[x][s];
}
vll maxpre(int i, vvvll &D) {
vll T(D[i-1].size(), 0LL);
for(int j = 0; j < D[i-1].size(); j++)
for(int k = 0; k <= j; k++)
T[j] = max(T[j], D[i-1][j][k]);
return T;
}
vll maxall(int i, vvvll &D) {
vll T(D[i-1].size(), 0LL);
for(int j = 0; j < D[i-1].size(); j++)
for(int k = 0; k < D[i-1][j].size(); k++)
T[j] = max(T[j], D[i-1][j][k]);
return T;
}
ll max_weights(int N, int M, vector<int> X, vector<int> Y, vector<int> W) {
bool subtask1 = true, subtask2 = true;
for(auto i : X) {
if(i%2) subtask1 = false;
if(i >= 2) subtask2 = false;
}
if(subtask1) {
ll ans = 0LL;
for(auto i : W) ans += i;
return ans;
}
if(subtask2) {
vector<pll> A, B;
for(int i = 0; i < M; i++) {
if(X[i]) B.push_back({Y[i], W[i]});
else A.push_back({Y[i], W[i]});
}
sort(A.begin(), A.end());
sort(B.begin(), B.end());
if(N == 2) {
ll a = 0LL, b = 0LL;
for(auto &i : A) a += i.se;
for(auto &i : B) b += i.se;
return max(a, b);
} else {
ll tmp = 0LL;
for(auto &i : B) tmp += i.se;
B.push_back({N, 0});
ll ans = tmp;
for(int i = 0, j = 0; i < B.size(); i++) {
while(j < A.size() && A[j].fi < B[i].fi) tmp += A[j++].se;
ans = max(ans, tmp);
tmp -= B[i].se;
}
return ans;
}
}
int MX = min(N, 2+*max_element(X.begin(), X.end()));
int MY = 2+*max_element(Y.begin(), Y.end());
vvll A(MX, vll(MY, 0LL));
for(int i = 0; i < M; i++) A[X[i]][Y[i]] += W[i];
vvll B(MX, vll(MY, 0LL));
for(int i = 0; i < MX; i++)
for(int j = 1; j < MY; j++)
B[i][j] = B[i][j-1]+A[i][j-1];
vvvll D(MX, vvll(MY, vll(MY, 0LL)));
for(int j = 0; j < MY; j++) {
for(int k = 0; k < MY; k++) {
if(j > k) D[1][j][k] = pre(2, 0, j, B)+pre(0, k, j, B);
else D[1][j][k] = pre(2, 0, j, B)+pre(1, j, k, B);
}
}
for(int i = 2; i < MX; i++) {
vll MP = maxpre(i, D), MA = maxall(i, D);
for(int j = 0; j < MY; j++) {
for(int k = 0; k < MY; k++)
D[i][j][0] = max(D[i][j][0], D[i-1][0][k]+pre(i-1, min(j, k), j, B)+pre(i+1, 0, j, B));
for(int k = 1; k < MY; k++) {
if(j > k) D[i][j][k] = MP[k]-pre(i, 0, k, B)+pre(i+1, 0, j, B)+pre(i-1, k, j, B);
else D[i][j][k] = MA[k]-pre(i, 0, j, B)+pre(i+1, 0, j, B);
}
}
}
//cout << " " << D;
ll ans = 0LL;
for(int i = 0; i < MY; i++)
for(int j = 0; j < MY; j++)
ans = max(ans, D.back()[i][j]);
return ans;
}
Compilation message
fish.cpp: In function 'll pre(int, int, int, vvll&)':
fish.cpp:24:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
24 | if(x < 0 || x >= B.size()) return 0LL;
| ~~^~~~~~~~~~~
fish.cpp: In function 'vll maxpre(int, vvvll&)':
fish.cpp:30:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
30 | for(int j = 0; j < D[i-1].size(); j++)
| ~~^~~~~~~~~~~~~~~
fish.cpp: In function 'vll maxall(int, vvvll&)':
fish.cpp:38:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
38 | for(int j = 0; j < D[i-1].size(); j++)
| ~~^~~~~~~~~~~~~~~
fish.cpp:39:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
39 | for(int k = 0; k < D[i-1][j].size(); k++)
| ~~^~~~~~~~~~~~~~~~~~
fish.cpp: In function 'll max_weights(int, int, std::vector<int>, std::vector<int>, std::vector<int>)':
fish.cpp:73:37: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
73 | for(int i = 0, j = 0; i < B.size(); i++) {
| ~~^~~~~~~~~~
fish.cpp:74:25: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
74 | while(j < A.size() && A[j].fi < B[i].fi) tmp += A[j++].se;
| ~~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
2132 KB |
Output is correct |
2 |
Correct |
27 ms |
2624 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
102 ms |
7276 KB |
Output is correct |
6 |
Correct |
83 ms |
7272 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
60 ms |
10048 KB |
Output is correct |
3 |
Correct |
75 ms |
11828 KB |
Output is correct |
4 |
Correct |
22 ms |
3536 KB |
Output is correct |
5 |
Correct |
33 ms |
4276 KB |
Output is correct |
6 |
Correct |
1 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 |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
30 ms |
5716 KB |
Output is correct |
13 |
Correct |
35 ms |
6572 KB |
Output is correct |
14 |
Correct |
31 ms |
5244 KB |
Output is correct |
15 |
Correct |
34 ms |
5700 KB |
Output is correct |
16 |
Correct |
30 ms |
5312 KB |
Output is correct |
17 |
Correct |
34 ms |
5696 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
50 ms |
26068 KB |
Output is correct |
3 |
Correct |
52 ms |
24560 KB |
Output is correct |
4 |
Correct |
48 ms |
24404 KB |
Output is correct |
5 |
Correct |
72 ms |
28372 KB |
Output is correct |
6 |
Correct |
63 ms |
28352 KB |
Output is correct |
7 |
Correct |
67 ms |
28348 KB |
Output is correct |
8 |
Correct |
85 ms |
28472 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 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 |
1 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 |
724 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
568 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
2 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 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 |
1 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 |
724 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
568 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
2 ms |
468 KB |
Output is correct |
15 |
Correct |
266 ms |
217436 KB |
Output is correct |
16 |
Correct |
6 ms |
4052 KB |
Output is correct |
17 |
Correct |
261 ms |
219852 KB |
Output is correct |
18 |
Correct |
278 ms |
219864 KB |
Output is correct |
19 |
Correct |
273 ms |
219944 KB |
Output is correct |
20 |
Correct |
300 ms |
219756 KB |
Output is correct |
21 |
Correct |
82 ms |
57900 KB |
Output is correct |
22 |
Correct |
268 ms |
221672 KB |
Output is correct |
23 |
Correct |
248 ms |
218296 KB |
Output is correct |
24 |
Correct |
265 ms |
219112 KB |
Output is correct |
25 |
Correct |
2 ms |
1612 KB |
Output is correct |
26 |
Correct |
239 ms |
218368 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 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 |
1 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 |
724 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
568 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
2 ms |
468 KB |
Output is correct |
15 |
Correct |
266 ms |
217436 KB |
Output is correct |
16 |
Correct |
6 ms |
4052 KB |
Output is correct |
17 |
Correct |
261 ms |
219852 KB |
Output is correct |
18 |
Correct |
278 ms |
219864 KB |
Output is correct |
19 |
Correct |
273 ms |
219944 KB |
Output is correct |
20 |
Correct |
300 ms |
219756 KB |
Output is correct |
21 |
Correct |
82 ms |
57900 KB |
Output is correct |
22 |
Correct |
268 ms |
221672 KB |
Output is correct |
23 |
Correct |
248 ms |
218296 KB |
Output is correct |
24 |
Correct |
265 ms |
219112 KB |
Output is correct |
25 |
Correct |
2 ms |
1612 KB |
Output is correct |
26 |
Correct |
239 ms |
218368 KB |
Output is correct |
27 |
Execution timed out |
1072 ms |
2097156 KB |
Time limit exceeded |
28 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
50 ms |
26068 KB |
Output is correct |
3 |
Correct |
52 ms |
24560 KB |
Output is correct |
4 |
Correct |
48 ms |
24404 KB |
Output is correct |
5 |
Correct |
72 ms |
28372 KB |
Output is correct |
6 |
Correct |
63 ms |
28352 KB |
Output is correct |
7 |
Correct |
67 ms |
28348 KB |
Output is correct |
8 |
Correct |
85 ms |
28472 KB |
Output is correct |
9 |
Runtime error |
739 ms |
2097152 KB |
Execution killed with signal 9 |
10 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
2132 KB |
Output is correct |
2 |
Correct |
27 ms |
2624 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
102 ms |
7276 KB |
Output is correct |
6 |
Correct |
83 ms |
7272 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
60 ms |
10048 KB |
Output is correct |
9 |
Correct |
75 ms |
11828 KB |
Output is correct |
10 |
Correct |
22 ms |
3536 KB |
Output is correct |
11 |
Correct |
33 ms |
4276 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
30 ms |
5716 KB |
Output is correct |
19 |
Correct |
35 ms |
6572 KB |
Output is correct |
20 |
Correct |
31 ms |
5244 KB |
Output is correct |
21 |
Correct |
34 ms |
5700 KB |
Output is correct |
22 |
Correct |
30 ms |
5312 KB |
Output is correct |
23 |
Correct |
34 ms |
5696 KB |
Output is correct |
24 |
Correct |
0 ms |
212 KB |
Output is correct |
25 |
Correct |
50 ms |
26068 KB |
Output is correct |
26 |
Correct |
52 ms |
24560 KB |
Output is correct |
27 |
Correct |
48 ms |
24404 KB |
Output is correct |
28 |
Correct |
72 ms |
28372 KB |
Output is correct |
29 |
Correct |
63 ms |
28352 KB |
Output is correct |
30 |
Correct |
67 ms |
28348 KB |
Output is correct |
31 |
Correct |
85 ms |
28472 KB |
Output is correct |
32 |
Correct |
1 ms |
212 KB |
Output is correct |
33 |
Correct |
0 ms |
212 KB |
Output is correct |
34 |
Correct |
0 ms |
212 KB |
Output is correct |
35 |
Correct |
1 ms |
212 KB |
Output is correct |
36 |
Correct |
0 ms |
212 KB |
Output is correct |
37 |
Correct |
0 ms |
212 KB |
Output is correct |
38 |
Correct |
0 ms |
212 KB |
Output is correct |
39 |
Correct |
0 ms |
212 KB |
Output is correct |
40 |
Correct |
1 ms |
340 KB |
Output is correct |
41 |
Correct |
2 ms |
724 KB |
Output is correct |
42 |
Correct |
1 ms |
468 KB |
Output is correct |
43 |
Correct |
1 ms |
568 KB |
Output is correct |
44 |
Correct |
1 ms |
340 KB |
Output is correct |
45 |
Correct |
2 ms |
468 KB |
Output is correct |
46 |
Correct |
266 ms |
217436 KB |
Output is correct |
47 |
Correct |
6 ms |
4052 KB |
Output is correct |
48 |
Correct |
261 ms |
219852 KB |
Output is correct |
49 |
Correct |
278 ms |
219864 KB |
Output is correct |
50 |
Correct |
273 ms |
219944 KB |
Output is correct |
51 |
Correct |
300 ms |
219756 KB |
Output is correct |
52 |
Correct |
82 ms |
57900 KB |
Output is correct |
53 |
Correct |
268 ms |
221672 KB |
Output is correct |
54 |
Correct |
248 ms |
218296 KB |
Output is correct |
55 |
Correct |
265 ms |
219112 KB |
Output is correct |
56 |
Correct |
2 ms |
1612 KB |
Output is correct |
57 |
Correct |
239 ms |
218368 KB |
Output is correct |
58 |
Execution timed out |
1072 ms |
2097156 KB |
Time limit exceeded |
59 |
Halted |
0 ms |
0 KB |
- |