/**
* Author : Nguyen Tuan Vu
* Created : 01.01.2023
**/
#pragma GCC optimize("O2")
#pragma GCC target("avx,avx2,fma")
#include<bits/stdc++.h>
#define MASK(x) ((1ll)<<(x))
#define BIT(x, i) (((x)>>(i))&(1))
#define ALL(v) (v).begin(), (v).end()
#define REP(i, n) for (int i = 0, _n = (n); i < _n; ++i)
#define FOR(i, a, b) for (int i = (a), _b = (b); i <= _b; ++i)
#define FORD(i, b, a) for (int i = (b), _a = (a); i >= _a; --i)
#define db(val) "["#val" = "<<(val)<<"] "
template <class X, class Y> bool minimize(X &a, Y b) {
if (a > b) return a = b, true;
return false;
}
template <class X, class Y> bool maximize(X &a, Y b) {
if (a < b) return a = b, true;
return false;
}
using namespace std;
mt19937 jdg(chrono::steady_clock::now().time_since_epoch().count());
int Rand(int l, int r) {return l + jdg() % (r - l + 1);}
const int N = 1e5 + 5;
namespace sub1 {
bool check(int m, vector <array <int, 3>> fishes) {
REP(i, m) {
if (fishes[i][0] & 1) return false;
}
return true;
}
long long solve(int m, vector <array <int, 3>> fishes) {
//assert(1 == 0);
long long ans = 0;
REP(i, m) ans += fishes[i][2];
return ans;
}
}
namespace sub2 {
bool check(int m, vector <array <int, 3>> fishes) {
REP(i, m) if (fishes[i][0] > 1) return false;
return true;
}
long long solve(int n, int m, vector <array <int, 3>> fishes) {
//assert(1 == 0);
if (n == 2) {
long long ans[2] = {0};
REP(i, m) ans[fishes[i][0]] += fishes[i][2];
return max(ans[0], ans[1]);
}
vector <long long> sum[2];
REP(i, 2) sum[i].resize(n + 7, 0);
REP(i, m) sum[fishes[i][0]][fishes[i][1]] += fishes[i][2];
FOR(i, 1, n - 1) REP(j, 2) sum[j][i] = sum[j][i - 1] + sum[j][i];
long long ans = sum[1][n - 1];
REP(i, n) maximize(ans, sum[0][i] + sum[1][n - 1] - sum[1][i]);
return ans;
}
}
namespace sub3 {
bool check(int m, vector <array <int, 3>> fishes) {
REP(i, m) if (fishes[i][1] != 0) return false;
return true;
}
int cost[N];
long long dp[N];
long long solve(int n, int m, vector <array <int, 3>> fishes) {
REP(i, m) cost[fishes[i][0] + 1] = fishes[i][2];
FOR(i, 1, n) {
maximize(dp[i], dp[i - 1]);
if (i >= 2) maximize(dp[i], dp[i - 2] + cost[i - 1]);
if (i > 3) maximize(dp[i], dp[i - 3] + cost[i - 1] + cost[i - 2]);
}
long long ans = 0;
FOR(i, 1, n) {
dp[i] = dp[i] + cost[i + 1];
maximize(ans, dp[i]);
}
return ans;
}
}
const long long INF = 1e18 + 7;
namespace sub6 {
vector <int> coor[N];
vector <long long> f[N], g[N];
vector <pair <int, long long>> sum[N];
long long get_sum(int i, int x) {
//cout << x << '\n';
//for (auto x : sum[i]) cout << x.first << ' ' << x.second << '\n';
int pos = upper_bound(ALL(sum[i]), make_pair(x, INF)) - sum[i].begin() - 1;
//cout << pos << '\n';
if (pos == -1) return 0;
return sum[i][pos].second;
}
long long solve(int n, int m, vector <array <int, 3>> fishes) {
REP(i, m) {
if (fishes[i][0] + 1 > 1) coor[fishes[i][0]].push_back(fishes[i][1] + 1);
if (fishes[i][0] + 1 < n) coor[fishes[i][0] + 2].push_back(fishes[i][1] + 1);
sum[fishes[i][0] + 1].push_back({fishes[i][1] + 1, fishes[i][2]});
}
//return 0;
FOR(i, 1, n) {
coor[i].push_back(0);
sort (ALL(coor[i]));
coor[i].erase(unique(ALL(coor[i])), coor[i].end());
f[i].resize(coor[i].size() + 5, -1);
g[i].resize(coor[i].size() + 5, -1);
sort (ALL(sum[i]));
FOR(j, 1, (int) sum[i].size() - 1) sum[i][j].second += sum[i][j - 1].second;
}
//cout << get_sum(1, 1) << '\n';
//return 0;
//for (auto x : sum[1]) cout << x.first << ' ' << x.second << '\n';
// f : roi
// g : chua
REP(i, coor[1].size()) g[1][i] = 0;
//cout << 1 << ' ';
//return 0;
FOR(i, 2, n) {
REP(k, coor[i - 1].size()) if (f[i - 1][k] != -1 || g[i - 1][k] != -1) {
REP(j, coor[i].size()) if (coor[i - 1][k] <= coor[i][j]) {
// f
if (f[i - 1][k] != -1) maximize(g[i][j], f[i - 1][k]);
// g
if (g[i - 1][k] != -1) {
long long R = get_sum(i - 1, coor[i][j]);
long long L = get_sum(i - 1, coor[i - 1][k]);
//if (i == 2) cout << coor[i - 1][k] << ' ' << coor[i][j] << ' ' << R - L << '\n';
maximize(g[i][j], g[i - 1][k] + R - L);
}
}
else {
// update f
maximize(f[i][j], max(f[i - 1][k], g[i - 1][k]) + get_sum(i, coor[i - 1][k]) - get_sum(i, coor[i][j]));
// update g
maximize(g[i][j], max(f[i - 1][k], g[i - 1][k]));
}
}
if (i > 2) {
REP(k, coor[i - 2].size()) if (f[i - 2][k] != -1 || g[i - 2][k] != -1) {
REP(j, coor[i].size()) {
maximize(g[i][j], max(f[i - 2][k], g[i - 2][k]) + get_sum(i - 1, max(coor[i - 2][k], coor[i][j])));
}
}
}
if (i > 3) {
REP(k, coor[i - 3].size()) if (f[i - 3][k] != -1 || g[i - 3][k] != -1) {
REP(j, coor[i].size()) {
maximize(g[i][j], max(f[i - 3][k], g[i - 3][k]) + get_sum(i - 1, coor[i][j]) + get_sum(i - 2, coor[i - 3][k]));
}
}
}
//cout << i << '\n';
//REP(j, coor[i].size()) {
// cout << j << ' ' << coor[i][j] << ' ' << f[i][j] << ' ' << g[i][j] << '\n';
//}
//cout << '\n';
}
long long ans = 0;
FOR(i, 1, n) REP(j, coor[i].size()) maximize(ans, max(f[i][j], g[i][j]) + get_sum(i + 1, coor[i][j]));
return ans;
}
};
long long max_weights(int n, int m, vector <int> X, vector <int> Y, vector <int> W) {
vector <array <int, 3>> fishes;
fishes.resize(m + 7);
REP(i, m) fishes[i] = {X[i], Y[i], W[i]};
if (sub1::check(m, fishes)) return sub1::solve(m, fishes);
else if (sub2::check(m, fishes)) return sub2::solve(n, m, fishes);
else if (sub3::check(m, fishes)) return sub3::solve(n, m, fishes);
else return sub6::solve(n, m, fishes);
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
29 ms |
14760 KB |
Output is correct |
2 |
Correct |
36 ms |
15604 KB |
Output is correct |
3 |
Correct |
6 ms |
9684 KB |
Output is correct |
4 |
Correct |
6 ms |
9684 KB |
Output is correct |
5 |
Correct |
95 ms |
25028 KB |
Output is correct |
6 |
Correct |
98 ms |
25052 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
9684 KB |
Output is correct |
2 |
Correct |
56 ms |
19996 KB |
Output is correct |
3 |
Correct |
68 ms |
21924 KB |
Output is correct |
4 |
Correct |
29 ms |
14692 KB |
Output is correct |
5 |
Correct |
34 ms |
15664 KB |
Output is correct |
6 |
Correct |
5 ms |
9684 KB |
Output is correct |
7 |
Correct |
6 ms |
9624 KB |
Output is correct |
8 |
Correct |
5 ms |
9684 KB |
Output is correct |
9 |
Correct |
5 ms |
9712 KB |
Output is correct |
10 |
Correct |
5 ms |
9708 KB |
Output is correct |
11 |
Correct |
7 ms |
9812 KB |
Output is correct |
12 |
Correct |
33 ms |
16116 KB |
Output is correct |
13 |
Correct |
36 ms |
17164 KB |
Output is correct |
14 |
Correct |
31 ms |
16148 KB |
Output is correct |
15 |
Correct |
37 ms |
16728 KB |
Output is correct |
16 |
Correct |
30 ms |
16100 KB |
Output is correct |
17 |
Correct |
34 ms |
16696 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
9708 KB |
Output is correct |
2 |
Correct |
6 ms |
10452 KB |
Output is correct |
3 |
Correct |
27 ms |
14172 KB |
Output is correct |
4 |
Correct |
17 ms |
12964 KB |
Output is correct |
5 |
Correct |
34 ms |
17120 KB |
Output is correct |
6 |
Correct |
29 ms |
16464 KB |
Output is correct |
7 |
Correct |
32 ms |
17120 KB |
Output is correct |
8 |
Correct |
34 ms |
17080 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
9600 KB |
Output is correct |
2 |
Correct |
6 ms |
9708 KB |
Output is correct |
3 |
Correct |
6 ms |
9684 KB |
Output is correct |
4 |
Correct |
6 ms |
9684 KB |
Output is correct |
5 |
Correct |
6 ms |
9684 KB |
Output is correct |
6 |
Correct |
6 ms |
9708 KB |
Output is correct |
7 |
Correct |
5 ms |
9684 KB |
Output is correct |
8 |
Correct |
6 ms |
9708 KB |
Output is correct |
9 |
Correct |
6 ms |
9684 KB |
Output is correct |
10 |
Correct |
7 ms |
9976 KB |
Output is correct |
11 |
Correct |
7 ms |
9720 KB |
Output is correct |
12 |
Correct |
7 ms |
9812 KB |
Output is correct |
13 |
Correct |
5 ms |
9684 KB |
Output is correct |
14 |
Correct |
7 ms |
9848 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
9600 KB |
Output is correct |
2 |
Correct |
6 ms |
9708 KB |
Output is correct |
3 |
Correct |
6 ms |
9684 KB |
Output is correct |
4 |
Correct |
6 ms |
9684 KB |
Output is correct |
5 |
Correct |
6 ms |
9684 KB |
Output is correct |
6 |
Correct |
6 ms |
9708 KB |
Output is correct |
7 |
Correct |
5 ms |
9684 KB |
Output is correct |
8 |
Correct |
6 ms |
9708 KB |
Output is correct |
9 |
Correct |
6 ms |
9684 KB |
Output is correct |
10 |
Correct |
7 ms |
9976 KB |
Output is correct |
11 |
Correct |
7 ms |
9720 KB |
Output is correct |
12 |
Correct |
7 ms |
9812 KB |
Output is correct |
13 |
Correct |
5 ms |
9684 KB |
Output is correct |
14 |
Correct |
7 ms |
9848 KB |
Output is correct |
15 |
Correct |
5 ms |
9684 KB |
Output is correct |
16 |
Correct |
27 ms |
9920 KB |
Output is correct |
17 |
Execution timed out |
1078 ms |
14700 KB |
Time limit exceeded |
18 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
9600 KB |
Output is correct |
2 |
Correct |
6 ms |
9708 KB |
Output is correct |
3 |
Correct |
6 ms |
9684 KB |
Output is correct |
4 |
Correct |
6 ms |
9684 KB |
Output is correct |
5 |
Correct |
6 ms |
9684 KB |
Output is correct |
6 |
Correct |
6 ms |
9708 KB |
Output is correct |
7 |
Correct |
5 ms |
9684 KB |
Output is correct |
8 |
Correct |
6 ms |
9708 KB |
Output is correct |
9 |
Correct |
6 ms |
9684 KB |
Output is correct |
10 |
Correct |
7 ms |
9976 KB |
Output is correct |
11 |
Correct |
7 ms |
9720 KB |
Output is correct |
12 |
Correct |
7 ms |
9812 KB |
Output is correct |
13 |
Correct |
5 ms |
9684 KB |
Output is correct |
14 |
Correct |
7 ms |
9848 KB |
Output is correct |
15 |
Correct |
5 ms |
9684 KB |
Output is correct |
16 |
Correct |
27 ms |
9920 KB |
Output is correct |
17 |
Execution timed out |
1078 ms |
14700 KB |
Time limit exceeded |
18 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
9708 KB |
Output is correct |
2 |
Correct |
6 ms |
10452 KB |
Output is correct |
3 |
Correct |
27 ms |
14172 KB |
Output is correct |
4 |
Correct |
17 ms |
12964 KB |
Output is correct |
5 |
Correct |
34 ms |
17120 KB |
Output is correct |
6 |
Correct |
29 ms |
16464 KB |
Output is correct |
7 |
Correct |
32 ms |
17120 KB |
Output is correct |
8 |
Correct |
34 ms |
17080 KB |
Output is correct |
9 |
Correct |
127 ms |
37560 KB |
Output is correct |
10 |
Correct |
98 ms |
29388 KB |
Output is correct |
11 |
Correct |
214 ms |
49112 KB |
Output is correct |
12 |
Correct |
6 ms |
9684 KB |
Output is correct |
13 |
Correct |
6 ms |
9708 KB |
Output is correct |
14 |
Correct |
6 ms |
9684 KB |
Output is correct |
15 |
Correct |
6 ms |
9704 KB |
Output is correct |
16 |
Correct |
6 ms |
9708 KB |
Output is correct |
17 |
Correct |
6 ms |
9684 KB |
Output is correct |
18 |
Correct |
6 ms |
9700 KB |
Output is correct |
19 |
Correct |
5 ms |
9640 KB |
Output is correct |
20 |
Correct |
6 ms |
10452 KB |
Output is correct |
21 |
Correct |
29 ms |
25340 KB |
Output is correct |
22 |
Correct |
153 ms |
37356 KB |
Output is correct |
23 |
Correct |
276 ms |
50860 KB |
Output is correct |
24 |
Correct |
276 ms |
51968 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
29 ms |
14760 KB |
Output is correct |
2 |
Correct |
36 ms |
15604 KB |
Output is correct |
3 |
Correct |
6 ms |
9684 KB |
Output is correct |
4 |
Correct |
6 ms |
9684 KB |
Output is correct |
5 |
Correct |
95 ms |
25028 KB |
Output is correct |
6 |
Correct |
98 ms |
25052 KB |
Output is correct |
7 |
Correct |
6 ms |
9684 KB |
Output is correct |
8 |
Correct |
56 ms |
19996 KB |
Output is correct |
9 |
Correct |
68 ms |
21924 KB |
Output is correct |
10 |
Correct |
29 ms |
14692 KB |
Output is correct |
11 |
Correct |
34 ms |
15664 KB |
Output is correct |
12 |
Correct |
5 ms |
9684 KB |
Output is correct |
13 |
Correct |
6 ms |
9624 KB |
Output is correct |
14 |
Correct |
5 ms |
9684 KB |
Output is correct |
15 |
Correct |
5 ms |
9712 KB |
Output is correct |
16 |
Correct |
5 ms |
9708 KB |
Output is correct |
17 |
Correct |
7 ms |
9812 KB |
Output is correct |
18 |
Correct |
33 ms |
16116 KB |
Output is correct |
19 |
Correct |
36 ms |
17164 KB |
Output is correct |
20 |
Correct |
31 ms |
16148 KB |
Output is correct |
21 |
Correct |
37 ms |
16728 KB |
Output is correct |
22 |
Correct |
30 ms |
16100 KB |
Output is correct |
23 |
Correct |
34 ms |
16696 KB |
Output is correct |
24 |
Correct |
5 ms |
9708 KB |
Output is correct |
25 |
Correct |
6 ms |
10452 KB |
Output is correct |
26 |
Correct |
27 ms |
14172 KB |
Output is correct |
27 |
Correct |
17 ms |
12964 KB |
Output is correct |
28 |
Correct |
34 ms |
17120 KB |
Output is correct |
29 |
Correct |
29 ms |
16464 KB |
Output is correct |
30 |
Correct |
32 ms |
17120 KB |
Output is correct |
31 |
Correct |
34 ms |
17080 KB |
Output is correct |
32 |
Correct |
7 ms |
9600 KB |
Output is correct |
33 |
Correct |
6 ms |
9708 KB |
Output is correct |
34 |
Correct |
6 ms |
9684 KB |
Output is correct |
35 |
Correct |
6 ms |
9684 KB |
Output is correct |
36 |
Correct |
6 ms |
9684 KB |
Output is correct |
37 |
Correct |
6 ms |
9708 KB |
Output is correct |
38 |
Correct |
5 ms |
9684 KB |
Output is correct |
39 |
Correct |
6 ms |
9708 KB |
Output is correct |
40 |
Correct |
6 ms |
9684 KB |
Output is correct |
41 |
Correct |
7 ms |
9976 KB |
Output is correct |
42 |
Correct |
7 ms |
9720 KB |
Output is correct |
43 |
Correct |
7 ms |
9812 KB |
Output is correct |
44 |
Correct |
5 ms |
9684 KB |
Output is correct |
45 |
Correct |
7 ms |
9848 KB |
Output is correct |
46 |
Correct |
5 ms |
9684 KB |
Output is correct |
47 |
Correct |
27 ms |
9920 KB |
Output is correct |
48 |
Execution timed out |
1078 ms |
14700 KB |
Time limit exceeded |
49 |
Halted |
0 ms |
0 KB |
- |