#include <bits/stdc++.h>
#ifndef LOCAL
#include <fish.h>
#endif // LOCAL
using namespace std;
#define rep(i, l, r) for(int i = (l), _r = (r); i < _r; ++i)
#define FOR(i, l, r) for(int i = (l), _r = (r); i <= _r; ++i)
#define ROF(i, r, l) for(int i = (r), _l = (l); i >= _l; --i)
#define all(v) begin(v), end(v)
#define compact(v) v.erase(unique(all(v)), end(v))
#define sz(v) (int)v.size()
#define dbg(x) "[" #x " = " << (x) << "]"
template<typename T>
bool minimize(T& a, const T& b){
if(a > b) return a = b, true; return false;
}
template<typename T>
bool maximize(T& a, const T& b){
if(a < b) return a = b, true; return false;
}
using ll = long long;
using ld = long double;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
template<typename T> T random_int(T l, T r){ return uniform_int_distribution<T>(l, r)(rng); }
template<typename T> T random_real(T l, T r){ return uniform_real_distribution<T>(l, r)(rng); }
const int MAX = 3e5 + 5;
int n, m;
struct fish{
int x, y, w;
} fishes[MAX];
namespace subtask1{
bool check(){
rep(i, 0, m) if(fishes[i].x& 1) return false;
return true;
}
ll solve(){
ll sum = 0;
rep(i, 0, m){
if((i > 0) || (i < n - 1)){
sum += fishes[i].w;
}
}
return sum;
}
}
namespace subtask2{
bool check(){
for(int i = 0; i < m; ++i){
if(fishes[i].x > 1) return false;
}
return true;
}
ll sum[3][MAX];
ll f[3][MAX], g[3][MAX];
ll solve(){
memset(f, 0, sizeof(f));
memset(g, 0, sizeof(g));
rep(i, 0, m){
sum[fishes[i].x][fishes[i].y] += fishes[i].w;
}
rep(i, 0, 2){
rep(j, 0, n){
sum[i][j] += (j > 0 ? sum[i][j - 1] : 0);
}
}
ll ans = max(sum[0][n - 1], sum[1][n - 1]);
if(n > 2){
rep(i, 0, n){
maximize(ans, sum[1][n - 1] - sum[1][i] + sum[0][i]);
}
}
return ans;
}
}
namespace subtask3{
bool check(){
rep(i, 0, m){
if(fishes[i].y > 0) return false;
}
return true;
}
ll dp[MAX], s[MAX];
ll solve(){
rep(i, 0, m) s[fishes[i].x] = fishes[i].w;
dp[0] = 0;
ll res = 0;
rep(i, 1, n){
dp[i] = max(dp[i - 1], s[i - 1]);
if(i >= 2) maximize(dp[i], dp[i - 2] + s[i - 1]);
if(i >= 3) maximize(dp[i], dp[i - 3] + s[i - 2] + s[i - 1]);
maximize(res, dp[i] + s[i + 1]);
}
return res;
}
}
namespace subtask45{
bool check(){
return n <= 300;
}
ll pref[305][305];
ll solve(){
rep(i, 0, m){
pref[fishes[i].x + 1][fishes[i].y + 1] += fishes[i].w;
}
FOR(i, 1, n){
FOR(j, 1, n){
pref[i][j] += pref[i][j - 1];
}
}
const ll inf = 1e18;
vector<vector<ll>> f(n + 1, vector<ll>(n + 1, -inf));
vector<vector<ll>> g(n + 1, vector<ll>(n + 1, -inf));
/*
let f[c][r] = the maximum sum of weights if we put a pier (c, r) but we don't consider the fishes (c, r') with r' >= r
g[c][r] = the maximum sum of weights if we put a pier (c, r) but we consider the fishes (c, r') with r' >= r
let R[i] the height of pier on column c the we put
consider when R[i - 1] < R[i] -> it contributes to f[c][r]
consider when R[i - 1] > R[i] -> it contributes to g[c][r]
when calculating f[c][r] :
- consider R[i - 2] > R[i - 1] < R[i]
- consider R[i - 2] < R[i - 1] < R[i]
*/
FOR(i, 0, n){
f[1][i] = g[1][i] = 0;
}
FOR(c, 2, n){
FOR(r, 0, n){
if(r == 0){
maximize(f[c][r], g[c - 1][r]);
}
FOR(k, 0, r){
maximize(f[c][r], f[c - 1][k] + pref[c - 1][r] - pref[c - 1][k]);
// cout << dbg(c) << dbg(r) << dbg(f[c - 1][k] + pref[c - 1][r] + pref[c - 1][k]) << '\n';
}
if(c > 2 && r > 0){
FOR(k, 1, n){
maximize(f[c][r], max(f[c - 2][k], g[c - 2][k]) + pref[c - 1][max(r, k)]);
// cout << dbg(c) << dbg(r) << dbg(max(f[c - 2][r], g[c - 2][r]) + pref[c - 1][max(r, k)]) << '\n';
}
}
FOR(k, r, n){
maximize(g[c][r], max(f[c - 1][k], g[c - 1][k]) + pref[c][k] - pref[c][r]);
// cout<< dbg(c) << dbg(r) << dbg(max(f[c - 1][k], g[c - 1][k]) + pref[c][k] - pref[c][r]) << '\n';
}
}
}
ll ans = 0;
FOR(c, 1, n){
FOR(r, 1, n){
maximize(ans, max(f[c][r], g[c][r]) + (c < n ? pref[c + 1][r] : 0));
}
}
// FOR(i, 1, n){
// FOR(j, 1, n){
// cout << pref[j][n - i + 1] << " \n"[j == n];
// }
// }
// cout << "F[r][c] : \n";
// FOR(i, 1, n){
// FOR(j, 1, n){
// cout << f[j][n - i + 1] << " \n"[j == n];
// }
// }
//
// cout << '\n' << "G[r][c] : \n";
// FOR(i, 1, n){
// FOR(j, 1, n){
// cout << g[j][n - i + 1] << " \n"[j == n];
// }
// }
// cerr << "subtask 4, 5 : ";
return ans;
}
}
namespace subtaskFull{
bool check(){
return true;
}
ll solve(){
vector<vector<pair<int, int>>> atColumn(n + 1);
vector<vector<ll>> pref(n + 1);
vector<vector<ll>> f(n + 1);
vector<vector<ll>> g(n + 1);
vector<int> nFishes(n + 1);
rep(i, 0, m){
int x = fishes[i].x + 1, y = fishes[i].y + 1, w = fishes[i].w;
atColumn[x].push_back({y, w});
}
const ll inf = 1e18;
FOR(i, 1, n){
atColumn[i].push_back({0, 0});
atColumn[i].push_back({n + 1, 0});
sort(all(atColumn[i]));
for(auto [j, c] : atColumn[i]){
}
nFishes[i] = sz(atColumn[i]);
pref[i].resize(nFishes[i]);
f[i].resize(nFishes[i], 0);
g[i].resize(nFishes[i], 0);
for(int j = 1; j < nFishes[i]; ++j){
pref[i][j] = pref[i][j - 1] + atColumn[i][j].second;
}
// cout << "column : " << i << '\n';
// for(int j = 0; j < nFishes[i]; ++j){
// cout << pref[i][j] << ' ';
// }
// cout << '\n';
}
auto sumPrefix = [&](int col, int row){
int pos = upper_bound(all(atColumn[col]), make_pair(row + 1, -1)) - atColumn[col].begin() - 1;
assert(-1 < pos && pos < sz(atColumn[col]));
return pref[col][pos];
};
FOR(c, 2, n){
long long mx = -inf;
for(int r = 0, i = 0; r < nFishes[c]; ++r){
if(r == 0){
maximize(f[c][r], g[c - 1][r]);
}
while(i < nFishes[c - 1] && atColumn[c - 1][i].first <= atColumn[c][r].first){
maximize(mx, f[c - 1][i] - pref[c - 1][i]);
++i;
}
assert(i > 0);
maximize(f[c][r], mx + sumPrefix(c - 1, atColumn[c][r].first));
}
if(c > 2){
mx = -inf;
//case when max(r, k) = k
for(int r = nFishes[c] - 1, i = nFishes[c - 2] - 1; r >= 0; --r){
while(i > 0 && atColumn[c - 2][i].first >= atColumn[c][i].first){
maximize(mx, max(f[c - 2][i], g[c - 2][i]) + sumPrefix(c - 1, atColumn[c - 2][i].first));
--i;
}
maximize(f[c][r], mx);
}
//case when max(r, k) = r
mx = -inf;
for(int r = 0, i = 1; r < nFishes[c]; ++r){
while(i < nFishes[c - 2] && atColumn[c - 2][i].first <= atColumn[c][r].first){
maximize(mx, max(f[c - 2][i], g[c - 2][i]) + sumPrefix(c - 1, atColumn[c][r].first));
++i;
}
maximize(f[c][r], mx);
}
}
mx = -inf;
for(int r = nFishes[c] - 1, i = nFishes[c - 1] - 1; r >= 0; --r){
while(i >= 0 && atColumn[c - 1][i].first >= atColumn[c][r].first){
maximize(mx, max(f[c - 1][i], g[c - 1][i]) + sumPrefix(c, atColumn[c - 1][i].first));
--i;
}
g[c][r] = mx - pref[c][r];
}
}
ll ans = 0;
FOR(c, 1, n){
// cout << "column : " << dbg(c) << '\n';
for(int r = 0; r < nFishes[c]; ++r){
// cout << "f(" << c << ", " << atColumn[c][r].first << ") = " << f[c][r] << '\n';
// cout << "g(" << c << ", " << atColumn[c][r].first << ") = " << g[c][r] << '\n';
// cout << dbg(f[c][r]) << ' ';
maximize(ans, max(f[c][r], g[c][r]) + (c == n ? 0 : sumPrefix(c + 1, atColumn[c][r].first)));
}
// cout << '\n';
}
return ans;
}
}
ll max_weights(int N, int M, vector<int> X, vector<int> Y, vector<int> W){
n = N;
m = M;
rep(i, 0, M) fishes[i] = {X[i], Y[i], W[i]};
if(subtask1::check()) return subtask1::solve();
if(subtask2::check()) return subtask2::solve();
if(subtask3::check()) return subtask3::solve();
if(subtask45::check()) return subtask45::solve();
if(subtaskFull::check()) return subtaskFull::solve();
return -1;
}
#ifdef LOCAL
int main(){
freopen("task.inp", "r", stdin);
freopen("task.out", "w", stdout);
int N, M;
cin >> N >> M;
vector<int> X(M), Y(M), W(M);
rep(i, 0, M){
cin >> X[i] >> Y[i] >> W[i];
}
cout << max_weights(N, M, X, Y, W) << '\n';
}
#endif // LOCAL
Compilation message
fish.cpp: In function 'bool minimize(T&, const T&)':
fish.cpp:18:5: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
18 | if(a > b) return a = b, true; return false;
| ^~
fish.cpp:18:35: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
18 | if(a > b) return a = b, true; return false;
| ^~~~~~
fish.cpp: In function 'bool maximize(T&, const T&)':
fish.cpp:23:5: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
23 | if(a < b) return a = b, true; return false;
| ^~
fish.cpp:23:35: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
23 | if(a < b) return a = b, true; return false;
| ^~~~~~
fish.cpp: In function 'll subtaskFull::solve()':
fish.cpp:239:22: warning: structured binding declaration set but not used [-Wunused-but-set-variable]
239 | for(auto [j, c] : atColumn[i]){
| ^~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
7760 KB |
Output is correct |
2 |
Correct |
20 ms |
8528 KB |
Output is correct |
3 |
Correct |
1 ms |
2384 KB |
Output is correct |
4 |
Correct |
1 ms |
2384 KB |
Output is correct |
5 |
Correct |
67 ms |
19048 KB |
Output is correct |
6 |
Correct |
67 ms |
19272 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
20816 KB |
Output is correct |
2 |
Correct |
36 ms |
29532 KB |
Output is correct |
3 |
Correct |
42 ms |
31196 KB |
Output is correct |
4 |
Correct |
18 ms |
7760 KB |
Output is correct |
5 |
Correct |
22 ms |
8528 KB |
Output is correct |
6 |
Correct |
3 ms |
20816 KB |
Output is correct |
7 |
Correct |
3 ms |
20816 KB |
Output is correct |
8 |
Correct |
3 ms |
21120 KB |
Output is correct |
9 |
Correct |
4 ms |
20816 KB |
Output is correct |
10 |
Correct |
1 ms |
2384 KB |
Output is correct |
11 |
Correct |
1 ms |
2384 KB |
Output is correct |
12 |
Correct |
21 ms |
26192 KB |
Output is correct |
13 |
Correct |
25 ms |
26960 KB |
Output is correct |
14 |
Correct |
32 ms |
26188 KB |
Output is correct |
15 |
Correct |
24 ms |
26696 KB |
Output is correct |
16 |
Correct |
21 ms |
26192 KB |
Output is correct |
17 |
Correct |
29 ms |
26440 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2384 KB |
Output is correct |
2 |
Correct |
2 ms |
6480 KB |
Output is correct |
3 |
Correct |
14 ms |
10716 KB |
Output is correct |
4 |
Correct |
11 ms |
10064 KB |
Output is correct |
5 |
Correct |
21 ms |
12624 KB |
Output is correct |
6 |
Correct |
17 ms |
11856 KB |
Output is correct |
7 |
Correct |
19 ms |
12500 KB |
Output is correct |
8 |
Correct |
20 ms |
12380 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2384 KB |
Output is correct |
2 |
Correct |
1 ms |
2384 KB |
Output is correct |
3 |
Correct |
3 ms |
20816 KB |
Output is correct |
4 |
Correct |
3 ms |
20816 KB |
Output is correct |
5 |
Correct |
2 ms |
20816 KB |
Output is correct |
6 |
Correct |
3 ms |
20944 KB |
Output is correct |
7 |
Correct |
3 ms |
21120 KB |
Output is correct |
8 |
Correct |
1 ms |
2384 KB |
Output is correct |
9 |
Correct |
9 ms |
4944 KB |
Output is correct |
10 |
Correct |
64 ms |
6224 KB |
Output is correct |
11 |
Correct |
8 ms |
4944 KB |
Output is correct |
12 |
Correct |
53 ms |
5968 KB |
Output is correct |
13 |
Correct |
3 ms |
4856 KB |
Output is correct |
14 |
Correct |
53 ms |
6176 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2384 KB |
Output is correct |
2 |
Correct |
1 ms |
2384 KB |
Output is correct |
3 |
Correct |
3 ms |
20816 KB |
Output is correct |
4 |
Correct |
3 ms |
20816 KB |
Output is correct |
5 |
Correct |
2 ms |
20816 KB |
Output is correct |
6 |
Correct |
3 ms |
20944 KB |
Output is correct |
7 |
Correct |
3 ms |
21120 KB |
Output is correct |
8 |
Correct |
1 ms |
2384 KB |
Output is correct |
9 |
Correct |
9 ms |
4944 KB |
Output is correct |
10 |
Correct |
64 ms |
6224 KB |
Output is correct |
11 |
Correct |
8 ms |
4944 KB |
Output is correct |
12 |
Correct |
53 ms |
5968 KB |
Output is correct |
13 |
Correct |
3 ms |
4856 KB |
Output is correct |
14 |
Correct |
53 ms |
6176 KB |
Output is correct |
15 |
Correct |
54 ms |
5968 KB |
Output is correct |
16 |
Correct |
3 ms |
4704 KB |
Output is correct |
17 |
Correct |
65 ms |
10040 KB |
Output is correct |
18 |
Correct |
64 ms |
9820 KB |
Output is correct |
19 |
Correct |
69 ms |
10012 KB |
Output is correct |
20 |
Correct |
65 ms |
9808 KB |
Output is correct |
21 |
Correct |
64 ms |
9808 KB |
Output is correct |
22 |
Correct |
73 ms |
11600 KB |
Output is correct |
23 |
Correct |
58 ms |
6480 KB |
Output is correct |
24 |
Correct |
65 ms |
9296 KB |
Output is correct |
25 |
Correct |
67 ms |
5968 KB |
Output is correct |
26 |
Correct |
58 ms |
6480 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2384 KB |
Output is correct |
2 |
Correct |
1 ms |
2384 KB |
Output is correct |
3 |
Correct |
3 ms |
20816 KB |
Output is correct |
4 |
Correct |
3 ms |
20816 KB |
Output is correct |
5 |
Correct |
2 ms |
20816 KB |
Output is correct |
6 |
Correct |
3 ms |
20944 KB |
Output is correct |
7 |
Correct |
3 ms |
21120 KB |
Output is correct |
8 |
Correct |
1 ms |
2384 KB |
Output is correct |
9 |
Correct |
9 ms |
4944 KB |
Output is correct |
10 |
Correct |
64 ms |
6224 KB |
Output is correct |
11 |
Correct |
8 ms |
4944 KB |
Output is correct |
12 |
Correct |
53 ms |
5968 KB |
Output is correct |
13 |
Correct |
3 ms |
4856 KB |
Output is correct |
14 |
Correct |
53 ms |
6176 KB |
Output is correct |
15 |
Correct |
54 ms |
5968 KB |
Output is correct |
16 |
Correct |
3 ms |
4704 KB |
Output is correct |
17 |
Correct |
65 ms |
10040 KB |
Output is correct |
18 |
Correct |
64 ms |
9820 KB |
Output is correct |
19 |
Correct |
69 ms |
10012 KB |
Output is correct |
20 |
Correct |
65 ms |
9808 KB |
Output is correct |
21 |
Correct |
64 ms |
9808 KB |
Output is correct |
22 |
Correct |
73 ms |
11600 KB |
Output is correct |
23 |
Correct |
58 ms |
6480 KB |
Output is correct |
24 |
Correct |
65 ms |
9296 KB |
Output is correct |
25 |
Correct |
67 ms |
5968 KB |
Output is correct |
26 |
Correct |
58 ms |
6480 KB |
Output is correct |
27 |
Incorrect |
3 ms |
3152 KB |
1st lines differ - on the 1st token, expected: '2999', found: '1999' |
28 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2384 KB |
Output is correct |
2 |
Correct |
2 ms |
6480 KB |
Output is correct |
3 |
Correct |
14 ms |
10716 KB |
Output is correct |
4 |
Correct |
11 ms |
10064 KB |
Output is correct |
5 |
Correct |
21 ms |
12624 KB |
Output is correct |
6 |
Correct |
17 ms |
11856 KB |
Output is correct |
7 |
Correct |
19 ms |
12500 KB |
Output is correct |
8 |
Correct |
20 ms |
12380 KB |
Output is correct |
9 |
Incorrect |
63 ms |
32072 KB |
1st lines differ - on the 1st token, expected: '99999', found: '66666' |
10 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
7760 KB |
Output is correct |
2 |
Correct |
20 ms |
8528 KB |
Output is correct |
3 |
Correct |
1 ms |
2384 KB |
Output is correct |
4 |
Correct |
1 ms |
2384 KB |
Output is correct |
5 |
Correct |
67 ms |
19048 KB |
Output is correct |
6 |
Correct |
67 ms |
19272 KB |
Output is correct |
7 |
Correct |
3 ms |
20816 KB |
Output is correct |
8 |
Correct |
36 ms |
29532 KB |
Output is correct |
9 |
Correct |
42 ms |
31196 KB |
Output is correct |
10 |
Correct |
18 ms |
7760 KB |
Output is correct |
11 |
Correct |
22 ms |
8528 KB |
Output is correct |
12 |
Correct |
3 ms |
20816 KB |
Output is correct |
13 |
Correct |
3 ms |
20816 KB |
Output is correct |
14 |
Correct |
3 ms |
21120 KB |
Output is correct |
15 |
Correct |
4 ms |
20816 KB |
Output is correct |
16 |
Correct |
1 ms |
2384 KB |
Output is correct |
17 |
Correct |
1 ms |
2384 KB |
Output is correct |
18 |
Correct |
21 ms |
26192 KB |
Output is correct |
19 |
Correct |
25 ms |
26960 KB |
Output is correct |
20 |
Correct |
32 ms |
26188 KB |
Output is correct |
21 |
Correct |
24 ms |
26696 KB |
Output is correct |
22 |
Correct |
21 ms |
26192 KB |
Output is correct |
23 |
Correct |
29 ms |
26440 KB |
Output is correct |
24 |
Correct |
1 ms |
2384 KB |
Output is correct |
25 |
Correct |
2 ms |
6480 KB |
Output is correct |
26 |
Correct |
14 ms |
10716 KB |
Output is correct |
27 |
Correct |
11 ms |
10064 KB |
Output is correct |
28 |
Correct |
21 ms |
12624 KB |
Output is correct |
29 |
Correct |
17 ms |
11856 KB |
Output is correct |
30 |
Correct |
19 ms |
12500 KB |
Output is correct |
31 |
Correct |
20 ms |
12380 KB |
Output is correct |
32 |
Correct |
1 ms |
2384 KB |
Output is correct |
33 |
Correct |
1 ms |
2384 KB |
Output is correct |
34 |
Correct |
3 ms |
20816 KB |
Output is correct |
35 |
Correct |
3 ms |
20816 KB |
Output is correct |
36 |
Correct |
2 ms |
20816 KB |
Output is correct |
37 |
Correct |
3 ms |
20944 KB |
Output is correct |
38 |
Correct |
3 ms |
21120 KB |
Output is correct |
39 |
Correct |
1 ms |
2384 KB |
Output is correct |
40 |
Correct |
9 ms |
4944 KB |
Output is correct |
41 |
Correct |
64 ms |
6224 KB |
Output is correct |
42 |
Correct |
8 ms |
4944 KB |
Output is correct |
43 |
Correct |
53 ms |
5968 KB |
Output is correct |
44 |
Correct |
3 ms |
4856 KB |
Output is correct |
45 |
Correct |
53 ms |
6176 KB |
Output is correct |
46 |
Correct |
54 ms |
5968 KB |
Output is correct |
47 |
Correct |
3 ms |
4704 KB |
Output is correct |
48 |
Correct |
65 ms |
10040 KB |
Output is correct |
49 |
Correct |
64 ms |
9820 KB |
Output is correct |
50 |
Correct |
69 ms |
10012 KB |
Output is correct |
51 |
Correct |
65 ms |
9808 KB |
Output is correct |
52 |
Correct |
64 ms |
9808 KB |
Output is correct |
53 |
Correct |
73 ms |
11600 KB |
Output is correct |
54 |
Correct |
58 ms |
6480 KB |
Output is correct |
55 |
Correct |
65 ms |
9296 KB |
Output is correct |
56 |
Correct |
67 ms |
5968 KB |
Output is correct |
57 |
Correct |
58 ms |
6480 KB |
Output is correct |
58 |
Incorrect |
3 ms |
3152 KB |
1st lines differ - on the 1st token, expected: '2999', found: '1999' |
59 |
Halted |
0 ms |
0 KB |
- |