/*
author: Maksim1744
created: 26.04.2024 01:52:26
*/
#include "bits/stdc++.h"
using namespace std;
using ll = long long;
using ld = long double;
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define sum(a) ( accumulate ((a).begin(), (a).end(), 0ll))
#define mine(a) (*min_element((a).begin(), (a).end()))
#define maxe(a) (*max_element((a).begin(), (a).end()))
#define mini(a) ( min_element((a).begin(), (a).end()) - (a).begin())
#define maxi(a) ( max_element((a).begin(), (a).end()) - (a).begin())
#define lowb(a, x) ( lower_bound((a).begin(), (a).end(), (x)) - (a).begin())
#define uppb(a, x) ( upper_bound((a).begin(), (a).end(), (x)) - (a).begin())
template<typename T> vector<T>& operator-- (vector<T> &v){for (auto& i : v) --i; return v;}
template<typename T> vector<T>& operator++ (vector<T> &v){for (auto& i : v) ++i; return v;}
template<typename T> istream& operator>>(istream& is, vector<T> &v){for (auto& i : v) is >> i; return is;}
template<typename T> ostream& operator<<(ostream& os, vector<T> v){for (auto& i : v) os << i << ' '; return os;}
template<typename T, typename U> pair<T,U>& operator-- (pair<T, U> &p){--p.first; --p.second; return p;}
template<typename T, typename U> pair<T,U>& operator++ (pair<T, U> &p){++p.first; ++p.second; return p;}
template<typename T, typename U> istream& operator>>(istream& is, pair<T, U> &p){is >> p.first >> p.second; return is;}
template<typename T, typename U> ostream& operator<<(ostream& os, pair<T, U> p){os << p.first << ' ' << p.second; return os;}
template<typename T, typename U> pair<T,U> operator-(pair<T,U> a, pair<T,U> b){return mp(a.first-b.first, a.second-b.second);}
template<typename T, typename U> pair<T,U> operator+(pair<T,U> a, pair<T,U> b){return mp(a.first+b.first, a.second+b.second);}
template<typename T, typename U> void umin(T& a, U b){if (a > b) a = b;}
template<typename T, typename U> void umax(T& a, U b){if (a < b) a = b;}
#ifdef HOME
#define SHOW_COLORS
#include "/mnt/c/Libs/tools/print.cpp"
#else
#define show(...) void(0)
#define debugf(fun) fun
#define debugv(var) var
#define mclock void(0)
#define shows void(0)
#define debug if (false)
#define OSTREAM(...) ;
#define OSTREAM0(...) ;
#endif
const ll inf = 1e18;
ll max_weights(int n, int m, std::vector<int> X, std::vector<int> Y, std::vector<int> W) {
vector<vector<pair<int, int>>> atx(n);
for (int i = 0; i < m; ++i) {
atx[X[i]].eb(Y[i], W[i]);
}
for (auto& v : atx)
sort(v.begin(), v.end());
vector<vector<ll>> up(n), down(n);
vector<ll> best(n);
for (int i = 0; i < n; ++i) {
up[i].assign(atx[i].size(), 0);
down[i].assign(atx[i].size(), 0);
if (i + 1 < n) {
int ind = 0;
ll prev = 0;
if (i >= 2) prev = max(prev, best[i - 2]);
if (i && !down[i - 1].empty()) prev = max(prev, maxe(down[i - 1]));
for (int j = 0; j < atx[i].size(); ++j) {
if (i) {
while (ind < atx[i - 1].size() && atx[i - 1][ind].first < atx[i][j].first) {
prev = max(prev, up[i - 1][ind]);
++ind;
}
}
if (j) up[i][j] = max(up[i][j], up[i][j - 1] + atx[i][j].second);
up[i][j] = max(up[i][j], prev + atx[i][j].second);
}
}
if (i > 0) {
int ind = (int)atx[i - 1].size() - 1;
ll prev = 0;
if (i >= 2) prev = max(prev, best[i - 2]);
for (int j = (int)atx[i].size() - 1; j >= 0; --j) {
if (i) {
while (ind >= 0 && atx[i - 1][ind].first > atx[i][j].first) {
prev = max(prev, down[i - 1][ind]);
--ind;
}
}
if (j + 1 < down[i].size())
down[i][j] = max(down[i][j], down[i][j + 1] + atx[i][j].second);
down[i][j] = max(down[i][j], prev + atx[i][j].second);
}
}
for (ll a : up[i]) best[i] = max(best[i], a);
for (ll a : down[i]) best[i] = max(best[i], a);
if (i) best[i] = max(best[i], best[i - 1]);
}
return best.back();
}
#ifdef HOUSE
int main() {
ios_base::sync_with_stdio(false); cin.tie(NULL);
int n, m;
cin >> n >> m;
vector<int> x(m), y(m), w(m);
for (int i = 0; i < m; ++i) {
cin >> x[i] >> y[i] >> w[i];
}
cout << max_weights(n, m, x, y, w) << '\n';
return 0;
}
#endif
Compilation message
fish.cpp: In function 'll max_weights(int, int, std::vector<int>, std::vector<int>, std::vector<int>)':
fish.cpp:73:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
73 | for (int j = 0; j < atx[i].size(); ++j) {
| ~~^~~~~~~~~~~~~~~
fish.cpp:75:32: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
75 | while (ind < atx[i - 1].size() && atx[i - 1][ind].first < atx[i][j].first) {
| ~~~~^~~~~~~~~~~~~~~~~~~
fish.cpp:95:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
95 | if (j + 1 < down[i].size())
| ~~~~~~^~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
30 ms |
12748 KB |
Output is correct |
2 |
Correct |
36 ms |
14792 KB |
Output is correct |
3 |
Correct |
4 ms |
8284 KB |
Output is correct |
4 |
Correct |
5 ms |
8028 KB |
Output is correct |
5 |
Correct |
98 ms |
28612 KB |
Output is correct |
6 |
Correct |
101 ms |
31588 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
53 ms |
17964 KB |
Output is correct |
3 |
Correct |
67 ms |
21176 KB |
Output is correct |
4 |
Correct |
28 ms |
13004 KB |
Output is correct |
5 |
Correct |
35 ms |
14640 KB |
Output is correct |
6 |
Correct |
0 ms |
600 KB |
Output is correct |
7 |
Correct |
0 ms |
600 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
5 ms |
8028 KB |
Output is correct |
11 |
Correct |
5 ms |
8028 KB |
Output is correct |
12 |
Correct |
28 ms |
12752 KB |
Output is correct |
13 |
Correct |
39 ms |
14792 KB |
Output is correct |
14 |
Correct |
29 ms |
12648 KB |
Output is correct |
15 |
Correct |
32 ms |
14480 KB |
Output is correct |
16 |
Correct |
29 ms |
12756 KB |
Output is correct |
17 |
Correct |
33 ms |
14008 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
8024 KB |
Output is correct |
2 |
Correct |
5 ms |
8028 KB |
Output is correct |
3 |
Correct |
33 ms |
14684 KB |
Output is correct |
4 |
Correct |
26 ms |
12892 KB |
Output is correct |
5 |
Correct |
45 ms |
21584 KB |
Output is correct |
6 |
Correct |
42 ms |
20828 KB |
Output is correct |
7 |
Correct |
44 ms |
21432 KB |
Output is correct |
8 |
Correct |
45 ms |
21328 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 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 |
600 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 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 |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 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 |
600 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 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 |
1 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 |
1 ms |
488 KB |
Output is correct |
17 |
Correct |
13 ms |
3400 KB |
Output is correct |
18 |
Correct |
13 ms |
3672 KB |
Output is correct |
19 |
Correct |
13 ms |
3512 KB |
Output is correct |
20 |
Correct |
16 ms |
3676 KB |
Output is correct |
21 |
Correct |
13 ms |
3420 KB |
Output is correct |
22 |
Correct |
25 ms |
7000 KB |
Output is correct |
23 |
Correct |
3 ms |
860 KB |
Output is correct |
24 |
Correct |
9 ms |
2452 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
3 ms |
860 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 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 |
600 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 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 |
1 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 |
1 ms |
488 KB |
Output is correct |
17 |
Correct |
13 ms |
3400 KB |
Output is correct |
18 |
Correct |
13 ms |
3672 KB |
Output is correct |
19 |
Correct |
13 ms |
3512 KB |
Output is correct |
20 |
Correct |
16 ms |
3676 KB |
Output is correct |
21 |
Correct |
13 ms |
3420 KB |
Output is correct |
22 |
Correct |
25 ms |
7000 KB |
Output is correct |
23 |
Correct |
3 ms |
860 KB |
Output is correct |
24 |
Correct |
9 ms |
2452 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
3 ms |
860 KB |
Output is correct |
27 |
Correct |
2 ms |
860 KB |
Output is correct |
28 |
Correct |
64 ms |
15952 KB |
Output is correct |
29 |
Correct |
87 ms |
21588 KB |
Output is correct |
30 |
Correct |
86 ms |
21332 KB |
Output is correct |
31 |
Correct |
85 ms |
21328 KB |
Output is correct |
32 |
Correct |
80 ms |
22100 KB |
Output is correct |
33 |
Correct |
86 ms |
21328 KB |
Output is correct |
34 |
Correct |
87 ms |
21328 KB |
Output is correct |
35 |
Correct |
33 ms |
8572 KB |
Output is correct |
36 |
Correct |
85 ms |
21860 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
8024 KB |
Output is correct |
2 |
Correct |
5 ms |
8028 KB |
Output is correct |
3 |
Correct |
33 ms |
14684 KB |
Output is correct |
4 |
Correct |
26 ms |
12892 KB |
Output is correct |
5 |
Correct |
45 ms |
21584 KB |
Output is correct |
6 |
Correct |
42 ms |
20828 KB |
Output is correct |
7 |
Correct |
44 ms |
21432 KB |
Output is correct |
8 |
Correct |
45 ms |
21328 KB |
Output is correct |
9 |
Correct |
42 ms |
21304 KB |
Output is correct |
10 |
Correct |
38 ms |
12884 KB |
Output is correct |
11 |
Correct |
73 ms |
25692 KB |
Output is correct |
12 |
Correct |
1 ms |
344 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
428 KB |
Output is correct |
17 |
Correct |
1 ms |
344 KB |
Output is correct |
18 |
Correct |
4 ms |
8284 KB |
Output is correct |
19 |
Correct |
6 ms |
8028 KB |
Output is correct |
20 |
Correct |
4 ms |
8028 KB |
Output is correct |
21 |
Correct |
5 ms |
8028 KB |
Output is correct |
22 |
Correct |
43 ms |
18776 KB |
Output is correct |
23 |
Correct |
70 ms |
25684 KB |
Output is correct |
24 |
Correct |
71 ms |
26332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
30 ms |
12748 KB |
Output is correct |
2 |
Correct |
36 ms |
14792 KB |
Output is correct |
3 |
Correct |
4 ms |
8284 KB |
Output is correct |
4 |
Correct |
5 ms |
8028 KB |
Output is correct |
5 |
Correct |
98 ms |
28612 KB |
Output is correct |
6 |
Correct |
101 ms |
31588 KB |
Output is correct |
7 |
Correct |
0 ms |
344 KB |
Output is correct |
8 |
Correct |
53 ms |
17964 KB |
Output is correct |
9 |
Correct |
67 ms |
21176 KB |
Output is correct |
10 |
Correct |
28 ms |
13004 KB |
Output is correct |
11 |
Correct |
35 ms |
14640 KB |
Output is correct |
12 |
Correct |
0 ms |
600 KB |
Output is correct |
13 |
Correct |
0 ms |
600 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
5 ms |
8028 KB |
Output is correct |
17 |
Correct |
5 ms |
8028 KB |
Output is correct |
18 |
Correct |
28 ms |
12752 KB |
Output is correct |
19 |
Correct |
39 ms |
14792 KB |
Output is correct |
20 |
Correct |
29 ms |
12648 KB |
Output is correct |
21 |
Correct |
32 ms |
14480 KB |
Output is correct |
22 |
Correct |
29 ms |
12756 KB |
Output is correct |
23 |
Correct |
33 ms |
14008 KB |
Output is correct |
24 |
Correct |
4 ms |
8024 KB |
Output is correct |
25 |
Correct |
5 ms |
8028 KB |
Output is correct |
26 |
Correct |
33 ms |
14684 KB |
Output is correct |
27 |
Correct |
26 ms |
12892 KB |
Output is correct |
28 |
Correct |
45 ms |
21584 KB |
Output is correct |
29 |
Correct |
42 ms |
20828 KB |
Output is correct |
30 |
Correct |
44 ms |
21432 KB |
Output is correct |
31 |
Correct |
45 ms |
21328 KB |
Output is correct |
32 |
Correct |
0 ms |
344 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
0 ms |
348 KB |
Output is correct |
35 |
Correct |
0 ms |
600 KB |
Output is correct |
36 |
Correct |
0 ms |
348 KB |
Output is correct |
37 |
Correct |
0 ms |
348 KB |
Output is correct |
38 |
Correct |
0 ms |
348 KB |
Output is correct |
39 |
Correct |
0 ms |
348 KB |
Output is correct |
40 |
Correct |
1 ms |
348 KB |
Output is correct |
41 |
Correct |
1 ms |
604 KB |
Output is correct |
42 |
Correct |
1 ms |
348 KB |
Output is correct |
43 |
Correct |
1 ms |
348 KB |
Output is correct |
44 |
Correct |
1 ms |
348 KB |
Output is correct |
45 |
Correct |
1 ms |
348 KB |
Output is correct |
46 |
Correct |
0 ms |
348 KB |
Output is correct |
47 |
Correct |
1 ms |
488 KB |
Output is correct |
48 |
Correct |
13 ms |
3400 KB |
Output is correct |
49 |
Correct |
13 ms |
3672 KB |
Output is correct |
50 |
Correct |
13 ms |
3512 KB |
Output is correct |
51 |
Correct |
16 ms |
3676 KB |
Output is correct |
52 |
Correct |
13 ms |
3420 KB |
Output is correct |
53 |
Correct |
25 ms |
7000 KB |
Output is correct |
54 |
Correct |
3 ms |
860 KB |
Output is correct |
55 |
Correct |
9 ms |
2452 KB |
Output is correct |
56 |
Correct |
1 ms |
348 KB |
Output is correct |
57 |
Correct |
3 ms |
860 KB |
Output is correct |
58 |
Correct |
2 ms |
860 KB |
Output is correct |
59 |
Correct |
64 ms |
15952 KB |
Output is correct |
60 |
Correct |
87 ms |
21588 KB |
Output is correct |
61 |
Correct |
86 ms |
21332 KB |
Output is correct |
62 |
Correct |
85 ms |
21328 KB |
Output is correct |
63 |
Correct |
80 ms |
22100 KB |
Output is correct |
64 |
Correct |
86 ms |
21328 KB |
Output is correct |
65 |
Correct |
87 ms |
21328 KB |
Output is correct |
66 |
Correct |
33 ms |
8572 KB |
Output is correct |
67 |
Correct |
85 ms |
21860 KB |
Output is correct |
68 |
Correct |
42 ms |
21304 KB |
Output is correct |
69 |
Correct |
38 ms |
12884 KB |
Output is correct |
70 |
Correct |
73 ms |
25692 KB |
Output is correct |
71 |
Correct |
1 ms |
344 KB |
Output is correct |
72 |
Correct |
1 ms |
344 KB |
Output is correct |
73 |
Correct |
1 ms |
348 KB |
Output is correct |
74 |
Correct |
0 ms |
348 KB |
Output is correct |
75 |
Correct |
1 ms |
428 KB |
Output is correct |
76 |
Correct |
1 ms |
344 KB |
Output is correct |
77 |
Correct |
4 ms |
8284 KB |
Output is correct |
78 |
Correct |
6 ms |
8028 KB |
Output is correct |
79 |
Correct |
4 ms |
8028 KB |
Output is correct |
80 |
Correct |
5 ms |
8028 KB |
Output is correct |
81 |
Correct |
43 ms |
18776 KB |
Output is correct |
82 |
Correct |
70 ms |
25684 KB |
Output is correct |
83 |
Correct |
71 ms |
26332 KB |
Output is correct |
84 |
Correct |
104 ms |
28336 KB |
Output is correct |
85 |
Correct |
94 ms |
28208 KB |
Output is correct |
86 |
Correct |
106 ms |
32084 KB |
Output is correct |
87 |
Correct |
110 ms |
33328 KB |
Output is correct |
88 |
Correct |
1 ms |
348 KB |
Output is correct |
89 |
Correct |
109 ms |
33384 KB |
Output is correct |
90 |
Correct |
100 ms |
30292 KB |
Output is correct |
91 |
Correct |
98 ms |
29268 KB |
Output is correct |