#include <bits/stdc++.h>
#define pb push_back
#define f first
#define sc second
using namespace std;
typedef long long int ll;
typedef string str;
ll max_weights(int n, int m, vector<int> X, vector<int> Y, vector<int> W){
vector<vector<pair<int, ll>>> v(n);
for(int i = 0; i < n; i++) v[i].pb({0, 0});
for(int i = 0; i < m; i++) v[X[i]].pb({Y[i]+1, W[i]});
for(auto &s: v) sort(s.begin(), s.end());
vector<vector<ll>> dp_up(n), dp_down, dtu2, pref;
vector<ll> dtu(n, 0);
for(int i = 0; i < n; i++) dp_up[i].resize(v[i].size(), 0);
dp_down = dp_up;
dtu2 = dp_up;
pref = dp_up;
for(int i = 0; i < n; i++) for(int j = 0; j < pref[i].size(); j++){
if(j) pref[i][j] = pref[i][j-1];
pref[i][j]+=v[i][j].sc;
}
for(int i = n-1; i >= 0; i--){
for(int j = 0; j < v[i].size(); j++){
if(i == n-1){
dp_down[i][j] = 0;
dp_up[i][j] = 0;
continue;
}
int in1 = lower_bound(v[i+1].begin(), v[i+1].end(), make_pair(v[i][j].f, -1LL))-v[i+1].begin();
if(in1 == v[i+1].size()) in1--;
dp_up[i][j]-=pref[i][j];
dp_down[i][j]-=pref[i+1].back()-pref[i+1][in1];
dp_up[i][j] = max(dp_up[i][j], pref[i].back()-pref[i][j]+dp_down[i+1].back());
if(i+2 < n){
int in2 = lower_bound(v[i+2].begin(), v[i+2].end(), make_pair(v[i][j].f, -1LL))-v[i+2].begin();
if(in2 == v[i+2].size()) in2--;
dp_down[i][j] = max(dp_down[i][j], dtu2[i+2][in2]+pref[i+1][in1]);
dp_down[i][j] = max(dp_down[i][j], dtu[i+2]-pref[i+1][in1]);
dp_down[i][j] = max(dp_down[i][j], pref[i+1].back()+dp_down[i+2].back());
}
}
if(i){
ll mx = 0, s = 0;
auto it = v[i-1].begin();
for(int j = 0; j < v[i].size(); j++){
while(it != v[i-1].end() && it->f <= v[i][j].f) s+=it->sc, it++;
mx = max(mx, s+dp_up[i][j]);
}
fill(dp_up[i-1].begin(), dp_up[i-1].end(), mx);
dtu[i] = mx;
mx = 0;
for(int j = 0; j < v[i].size(); j++) mx = max(mx, pref[i].back()-pref[i][j]+dp_down[i][j]);
fill(dp_down[i-1].begin(), dp_down[i-1].end(), mx);
for(int j = 0; j < v[i].size(); j++){
if(j) dtu2[i][j] = dtu2[i][j-1];
dtu2[i][j] = max(dtu2[i][j], dp_up[i][j]);
}
}
}
return max(*max_element(dp_up[0].begin(), dp_up[0].end()), *max_element(dp_down[0].begin(), dp_down[0].end()));
}
Compilation message
fish.cpp: In function 'll max_weights(int, int, std::vector<int>, std::vector<int>, std::vector<int>)':
fish.cpp:24:49: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
24 | for(int i = 0; i < n; i++) for(int j = 0; j < pref[i].size(); j++){
| ~~^~~~~~~~~~~~~~~~
fish.cpp:30:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
30 | for(int j = 0; j < v[i].size(); j++){
| ~~^~~~~~~~~~~~~
fish.cpp:38:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
38 | if(in1 == v[i+1].size()) in1--;
| ~~~~^~~~~~~~~~~~~~~~
fish.cpp:46:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
46 | if(in2 == v[i+2].size()) in2--;
| ~~~~^~~~~~~~~~~~~~~~
fish.cpp:56:30: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
56 | for(int j = 0; j < v[i].size(); j++){
| ~~^~~~~~~~~~~~~
fish.cpp:64:30: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
64 | for(int j = 0; j < v[i].size(); j++) mx = max(mx, pref[i].back()-pref[i][j]+dp_down[i][j]);
| ~~^~~~~~~~~~~~~
fish.cpp:67:30: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
67 | for(int j = 0; j < v[i].size(); j++){
| ~~^~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
67 ms |
31312 KB |
Output is correct |
2 |
Correct |
85 ms |
35520 KB |
Output is correct |
3 |
Correct |
39 ms |
28364 KB |
Output is correct |
4 |
Correct |
37 ms |
28456 KB |
Output is correct |
5 |
Correct |
163 ms |
55944 KB |
Output is correct |
6 |
Correct |
224 ms |
55844 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
212 KB |
1st lines differ - on the 1st token, expected: '2', found: '1' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
37 ms |
28472 KB |
Output is correct |
2 |
Incorrect |
43 ms |
28384 KB |
1st lines differ - on the 1st token, expected: '882019', found: '0' |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
212 KB |
1st lines differ - on the 1st token, expected: '3', found: '2' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
212 KB |
1st lines differ - on the 1st token, expected: '3', found: '2' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
212 KB |
1st lines differ - on the 1st token, expected: '3', found: '2' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
37 ms |
28472 KB |
Output is correct |
2 |
Incorrect |
43 ms |
28384 KB |
1st lines differ - on the 1st token, expected: '882019', found: '0' |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
67 ms |
31312 KB |
Output is correct |
2 |
Correct |
85 ms |
35520 KB |
Output is correct |
3 |
Correct |
39 ms |
28364 KB |
Output is correct |
4 |
Correct |
37 ms |
28456 KB |
Output is correct |
5 |
Correct |
163 ms |
55944 KB |
Output is correct |
6 |
Correct |
224 ms |
55844 KB |
Output is correct |
7 |
Incorrect |
0 ms |
212 KB |
1st lines differ - on the 1st token, expected: '2', found: '1' |
8 |
Halted |
0 ms |
0 KB |
- |