#include "fish.h"
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast,fast-math,unroll-loops")
#pragma GCC target("avx2,fma")
using namespace std;
int n;
vector<vector<long long>> nxt;
vector<vector<long long>> pre;
vector<vector<vector<long long>>> dp;
vector<vector<pair<int, int>>> in_col;
long long max_weights(int N, int M, vector<int> X, vector<int> Y, vector<int> W) {
n = N;
in_col.resize(N);
for (int i = 0; i < M; ++i) {
in_col[X[i]].push_back({Y[i], W[i]});
}
for (int i = 0; i < N; ++i) {
sort(in_col[i].begin(), in_col[i].end());
in_col[i].push_back({N, 0});
}
pre.resize(N);
for (int col = 1; col < N; ++col) {
pre[col].resize(in_col[col].size());
for (int i = 0; i < (int) in_col[col].size(); ++i) {
int ptr = 0;
long long sum = 0;
while (ptr < (int) in_col[col - 1].size() && in_col[col - 1][ptr].first < in_col[col][i].first) {
sum += in_col[col - 1][ptr].second;
ptr++;
}
pre[col][i] = sum;
}
}
nxt.resize(N);
for (int col = 0; col < N - 1; ++col) {
nxt[col].resize(in_col[col].size());
for (int i = 0; i < (int) in_col[col].size(); ++i) {//M tot
int ptr = 0;
long long sum = 0;
while (ptr < (int) in_col[col + 1].size() && in_col[col + 1][ptr].first < in_col[col][i].first) {
sum += in_col[col + 1][ptr].second;
ptr++;
}
nxt[col][i] = sum;
}
}
dp.resize(N + 1);
for (int i = 0; i < N + 1; ++i) {
dp[i].resize(2);
dp[i][0].assign(2, -1);
dp[i][1].assign(2, -1);
}
dp[0][0][0] = 0;
for (int col = 0; col < N; ++col) {
int sz = (int) in_col[col].size();
for (int f = 0; f < 2; ++f) {
for (int f2 = 0; f2 < 2; ++f2) {
if (dp[col][f][f2] == -1) {
continue;
}
if (f) {
long long s = 0;
for (int i = 0; i < sz; ++i) {
long long w = s;
if (col < N - 1) {
w += nxt[col][i];
}
dp[col + 1][1][0] = max(dp[col + 1][1][0], dp[col][f][f2] + w);
s -= in_col[col][i].second;
}
dp[col + 1][0][0] = max(dp[col + 1][0][0], dp[col][f][f2]);
} else {
long long s = 0;
for (int i = 0; i < sz; ++i) {
long long w = s;
if (f2) {
w += pre[col][i];
}
dp[col + 1][0][1] = max(dp[col + 1][0][1], dp[col][f][f2] + w);
s -= in_col[col][i].second;
}
s = 0;
if (col < N - 1) {
s = nxt[col][sz - 1];
}
if (f2) {
s += pre[col][sz - 1];
}
dp[col + 1][1][0] = max(dp[col + 1][1][0], dp[col][f][f2] + s);
}
}
}
}
long long ans = 0;
for (int f = 0; f < 2; ++f) {
for (int f2 = 0; f2 < 2; ++f2) {
ans = max(ans, dp[n][f][f2]);
}
}
return ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
48 ms |
32708 KB |
Output is correct |
2 |
Correct |
68 ms |
36536 KB |
Output is correct |
3 |
Correct |
29 ms |
31612 KB |
Output is correct |
4 |
Correct |
30 ms |
31580 KB |
Output is correct |
5 |
Correct |
116 ms |
49080 KB |
Output is correct |
6 |
Correct |
109 ms |
48980 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Execution timed out |
1049 ms |
14568 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
28 ms |
31688 KB |
Output is correct |
2 |
Correct |
29 ms |
31580 KB |
Output is correct |
3 |
Correct |
42 ms |
30504 KB |
Output is correct |
4 |
Correct |
39 ms |
32896 KB |
Output is correct |
5 |
Correct |
51 ms |
34900 KB |
Output is correct |
6 |
Correct |
50 ms |
34860 KB |
Output is correct |
7 |
Correct |
67 ms |
34896 KB |
Output is correct |
8 |
Correct |
51 ms |
34900 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
432 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
600 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
580 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
432 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
600 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
580 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
604 KB |
Output is correct |
17 |
Correct |
15 ms |
3164 KB |
Output is correct |
18 |
Correct |
14 ms |
3268 KB |
Output is correct |
19 |
Correct |
13 ms |
3420 KB |
Output is correct |
20 |
Correct |
16 ms |
3416 KB |
Output is correct |
21 |
Correct |
13 ms |
3420 KB |
Output is correct |
22 |
Correct |
36 ms |
6148 KB |
Output is correct |
23 |
Correct |
3 ms |
1116 KB |
Output is correct |
24 |
Correct |
9 ms |
2396 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
2 ms |
860 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
432 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
600 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
580 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
604 KB |
Output is correct |
17 |
Correct |
15 ms |
3164 KB |
Output is correct |
18 |
Correct |
14 ms |
3268 KB |
Output is correct |
19 |
Correct |
13 ms |
3420 KB |
Output is correct |
20 |
Correct |
16 ms |
3416 KB |
Output is correct |
21 |
Correct |
13 ms |
3420 KB |
Output is correct |
22 |
Correct |
36 ms |
6148 KB |
Output is correct |
23 |
Correct |
3 ms |
1116 KB |
Output is correct |
24 |
Correct |
9 ms |
2396 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
2 ms |
860 KB |
Output is correct |
27 |
Correct |
2 ms |
1372 KB |
Output is correct |
28 |
Correct |
99 ms |
14100 KB |
Output is correct |
29 |
Correct |
259 ms |
16468 KB |
Output is correct |
30 |
Correct |
80 ms |
15992 KB |
Output is correct |
31 |
Correct |
82 ms |
15952 KB |
Output is correct |
32 |
Correct |
136 ms |
17012 KB |
Output is correct |
33 |
Correct |
82 ms |
15956 KB |
Output is correct |
34 |
Correct |
105 ms |
15952 KB |
Output is correct |
35 |
Correct |
29 ms |
6992 KB |
Output is correct |
36 |
Correct |
96 ms |
16744 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
28 ms |
31688 KB |
Output is correct |
2 |
Correct |
29 ms |
31580 KB |
Output is correct |
3 |
Correct |
42 ms |
30504 KB |
Output is correct |
4 |
Correct |
39 ms |
32896 KB |
Output is correct |
5 |
Correct |
51 ms |
34900 KB |
Output is correct |
6 |
Correct |
50 ms |
34860 KB |
Output is correct |
7 |
Correct |
67 ms |
34896 KB |
Output is correct |
8 |
Correct |
51 ms |
34900 KB |
Output is correct |
9 |
Correct |
49 ms |
33876 KB |
Output is correct |
10 |
Correct |
41 ms |
19280 KB |
Output is correct |
11 |
Correct |
83 ms |
38480 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
29 ms |
31576 KB |
Output is correct |
19 |
Correct |
29 ms |
31804 KB |
Output is correct |
20 |
Correct |
34 ms |
31576 KB |
Output is correct |
21 |
Correct |
30 ms |
31708 KB |
Output is correct |
22 |
Correct |
57 ms |
34584 KB |
Output is correct |
23 |
Correct |
88 ms |
38336 KB |
Output is correct |
24 |
Correct |
80 ms |
38480 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
48 ms |
32708 KB |
Output is correct |
2 |
Correct |
68 ms |
36536 KB |
Output is correct |
3 |
Correct |
29 ms |
31612 KB |
Output is correct |
4 |
Correct |
30 ms |
31580 KB |
Output is correct |
5 |
Correct |
116 ms |
49080 KB |
Output is correct |
6 |
Correct |
109 ms |
48980 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Execution timed out |
1049 ms |
14568 KB |
Time limit exceeded |
9 |
Halted |
0 ms |
0 KB |
- |