#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define vll vector<ll>
#define pll pair<ll,ll>
#define vpll vector<pll>
#define S second
#define F first
long long max_weights(int n,int m,vector<int> X,vector<int> Y,vector<int> W) {
vector<vll> v (n+5,vll {-1}), w(n+5,vll {0});
vector<vpll> la (n+5,vpll {{-1,0}});
ll r=0;
for(int i=0;i<m;i++){
if(X[i]>0)la[X[i]].push_back({Y[i],0});
la[X[i]+1].push_back({Y[i],-W[i]});
la[X[i]+1].push_back({Y[i]-1,0});
la[X[i]+2].push_back({Y[i],0});
}
for(int i=0;i<=n;i++){
sort(la[i].begin(),la[i].end());
for(int j=1;j<la[i].size();j++){
if(la[i][j].F==la[i][j-1].F)continue;
v[i].push_back(la[i][j].F);
w[i].push_back(-la[i][j].S+w[i].back());
//cout<<v[i].back()<<"->"<<w[i].back()<<"\n";
}//cout<<"\n\n";
}
vll t1{{}},t2{{}},t3{{}},t4{{}};
vll h2[2]={{},{}},h1[2]={{0},{0}};
for(int i=1;i<=n;i++){
t1=h1[0];
for(int j=0;j<h1[0].size();j++){
t1[j]=t1[j]-w[i-1][j];
if(j>0)t1[j]=max(t1[j],t1[j-1]); //dp[i-1][k<j][0]-co[i][k<j]
}
t2=h2[1];
for(int j=0;j<h2[0].size();j++){
if(j>0)t2[j]=max(t2[j],t2[j-1]);//dp[i-2][k<j][1]
}
t3=h2[1];ll p=v[i-1].size()-1;
for(int j=h2[0].size()-1;j>=0;j--){
while(p>=0 && v[i-1][p]>v[i-2][j])p--;
//cerr<<t3[j]<<" ";
if(p>-1)t3[j]=t3[j]+w[i-1][p]; //dp[]
// cerr<<t3[j]<<" ";
if(j+1<h2[0].size())t3[j]=max(t3[j],t3[j+1]);
}//cerr<<"///////////////\n";
t4=h1[1];p=v[i].size()-1;
for(int j=h1[1].size()-1;j>=0;j--){
while(p>=0 && v[i][p]>v[i-1][j])p--;
if(p>-1)t4[j]=t4[j]+w[i][p];
if(j+1<h1[1].size())t4[j]=max(t4[j],t4[j+1]);
}
ll p1=-1,p2=-1;
vll h[2]={{},{}};
//cerr<<i<<"{\n";
for(int j=0;j<v[i].size();j++){
// cerr<<" "<<v[i][j]<<":\n";
ll dp[2]={0,0};
if(i>=2){
while(p1+1<v[i-1].size() && v[i-1][p1+1]<=v[i][j])p1++;
while(p2+1<v[i-2].size() && v[i-1][p2+1]<=v[i][j])p2++;
if(p1>-1) dp[0]=max(dp[0],t1[p1]+w[i-1][p1]);//dp[i-1][k][0]-co[i-1][k]+co[i-1][j]
//cerr<<"\t"<<dp[0]<<" "<<dp[1]<<"\n";
if(p2>-1) dp[0]=max(dp[0],t2[p2]+w[i-1][p1]);//dp[i-2][k][1] +co[i-1][j]
//cerr<<"\t"<<dp[0]<<" "<<dp[1]<<"\n";
if(p2<v[i-2].size()-1)dp[0]=max(dp[0],t3[p2+1]); //dp[i-2][k][1]+co[i-1][k]
dp[1]=dp[0];
//cerr<<"\t"<<dp[0]<<" "<<dp[1]<<"\n";
if(p1<v[i-1].size()-1)dp[1]=max(dp[1],t4[p2+1]-w[i][j]); //dp[i-1][k][1]+co [i] [k]-co[i][j]
}
//cerr<<"\t"<<dp[0]<<" "<<dp[1]<<"\n";
h[0].push_back(dp[0]);
h[1].push_back(dp[1]);
r=max(h[0].back(),max(r,h[1].back()));
}
// cerr<<"}\n";
h2[0]=h1[0];h1[0]=h[0];
h2[1]=h1[1];h1[1]=h[1];
}
return r;
}/*
int main() {
int N, M;
assert(2 == scanf("%d %d", &N, &M));
std::vector<int> X(M), Y(M), W(M);
for (int i = 0; i < M; ++i) {
assert(3 == scanf("%d %d %d", &X[i], &Y[i], &W[i]));
}
long long result = max_weights(N, M, X, Y, W);
printf("%lld\n", result);
return 0;
}
//*/
/*
0 0 0 1 0
0 0 3 0 0
5 0 0 0 0
0 2 0 0 0
0 0 0 0 0
*/
Compilation message
fish.cpp: In function 'long long int max_weights(int, int, std::vector<int>, std::vector<int>, std::vector<int>)':
fish.cpp:21:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
21 | for(int j=1;j<la[i].size();j++){
| ~^~~~~~~~~~~~~
fish.cpp:32:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
32 | for(int j=0;j<h1[0].size();j++){
| ~^~~~~~~~~~~~~
fish.cpp:37:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
37 | for(int j=0;j<h2[0].size();j++){
| ~^~~~~~~~~~~~~
fish.cpp:46:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
46 | if(j+1<h2[0].size())t3[j]=max(t3[j],t3[j+1]);
| ~~~^~~~~~~~~~~~~
fish.cpp:52:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
52 | if(j+1<h1[1].size())t4[j]=max(t4[j],t4[j+1]);
| ~~~^~~~~~~~~~~~~
fish.cpp:57:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
57 | for(int j=0;j<v[i].size();j++){
| ~^~~~~~~~~~~~
fish.cpp:61:27: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
61 | while(p1+1<v[i-1].size() && v[i-1][p1+1]<=v[i][j])p1++;
| ~~~~^~~~~~~~~~~~~~
fish.cpp:62:27: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
62 | while(p2+1<v[i-2].size() && v[i-1][p2+1]<=v[i][j])p2++;
| ~~~~^~~~~~~~~~~~~~
fish.cpp:67:22: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
67 | if(p2<v[i-2].size()-1)dp[0]=max(dp[0],t3[p2+1]); //dp[i-2][k][1]+co[i-1][k]
| ~~^~~~~~~~~~~~~~~~
fish.cpp:70:22: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
70 | if(p1<v[i-1].size()-1)dp[1]=max(dp[1],t4[p2+1]-w[i][j]); //dp[i-1][k][1]+co [i] [k]-co[i][j]
| ~~^~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
65 ms |
32928 KB |
Output is correct |
2 |
Correct |
74 ms |
37508 KB |
Output is correct |
3 |
Correct |
22 ms |
16732 KB |
Output is correct |
4 |
Correct |
24 ms |
16732 KB |
Output is correct |
5 |
Correct |
223 ms |
78348 KB |
Output is correct |
6 |
Correct |
267 ms |
83540 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
121 ms |
44316 KB |
Output is correct |
3 |
Correct |
134 ms |
51212 KB |
Output is correct |
4 |
Correct |
66 ms |
32964 KB |
Output is correct |
5 |
Correct |
74 ms |
37816 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 |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
22 ms |
16740 KB |
Output is correct |
11 |
Correct |
23 ms |
16732 KB |
Output is correct |
12 |
Correct |
73 ms |
37424 KB |
Output is correct |
13 |
Correct |
86 ms |
42392 KB |
Output is correct |
14 |
Correct |
73 ms |
33844 KB |
Output is correct |
15 |
Correct |
72 ms |
33372 KB |
Output is correct |
16 |
Correct |
70 ms |
35020 KB |
Output is correct |
17 |
Correct |
76 ms |
38436 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
27 ms |
16728 KB |
Output is correct |
2 |
Correct |
25 ms |
16728 KB |
Output is correct |
3 |
Correct |
51 ms |
23924 KB |
Output is correct |
4 |
Correct |
45 ms |
22796 KB |
Output is correct |
5 |
Correct |
80 ms |
33200 KB |
Output is correct |
6 |
Correct |
79 ms |
32532 KB |
Output is correct |
7 |
Correct |
78 ms |
33108 KB |
Output is correct |
8 |
Correct |
99 ms |
33108 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 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 |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
388 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 |
2 ms |
860 KB |
Output is correct |
11 |
Correct |
1 ms |
448 KB |
Output is correct |
12 |
Correct |
1 ms |
604 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
604 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 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 |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
388 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 |
2 ms |
860 KB |
Output is correct |
11 |
Correct |
1 ms |
448 KB |
Output is correct |
12 |
Correct |
1 ms |
604 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
604 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
604 KB |
Output is correct |
17 |
Correct |
22 ms |
7004 KB |
Output is correct |
18 |
Correct |
21 ms |
8028 KB |
Output is correct |
19 |
Correct |
23 ms |
8032 KB |
Output is correct |
20 |
Correct |
20 ms |
7868 KB |
Output is correct |
21 |
Correct |
20 ms |
7772 KB |
Output is correct |
22 |
Correct |
42 ms |
14732 KB |
Output is correct |
23 |
Correct |
5 ms |
2136 KB |
Output is correct |
24 |
Correct |
15 ms |
5380 KB |
Output is correct |
25 |
Correct |
1 ms |
604 KB |
Output is correct |
26 |
Correct |
5 ms |
2140 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 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 |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
388 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 |
2 ms |
860 KB |
Output is correct |
11 |
Correct |
1 ms |
448 KB |
Output is correct |
12 |
Correct |
1 ms |
604 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
604 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
604 KB |
Output is correct |
17 |
Correct |
22 ms |
7004 KB |
Output is correct |
18 |
Correct |
21 ms |
8028 KB |
Output is correct |
19 |
Correct |
23 ms |
8032 KB |
Output is correct |
20 |
Correct |
20 ms |
7868 KB |
Output is correct |
21 |
Correct |
20 ms |
7772 KB |
Output is correct |
22 |
Correct |
42 ms |
14732 KB |
Output is correct |
23 |
Correct |
5 ms |
2136 KB |
Output is correct |
24 |
Correct |
15 ms |
5380 KB |
Output is correct |
25 |
Correct |
1 ms |
604 KB |
Output is correct |
26 |
Correct |
5 ms |
2140 KB |
Output is correct |
27 |
Correct |
3 ms |
1372 KB |
Output is correct |
28 |
Correct |
102 ms |
33936 KB |
Output is correct |
29 |
Correct |
146 ms |
45068 KB |
Output is correct |
30 |
Correct |
167 ms |
62052 KB |
Output is correct |
31 |
Correct |
167 ms |
61928 KB |
Output is correct |
32 |
Correct |
138 ms |
49236 KB |
Output is correct |
33 |
Correct |
166 ms |
62036 KB |
Output is correct |
34 |
Correct |
165 ms |
62056 KB |
Output is correct |
35 |
Correct |
59 ms |
22868 KB |
Output is correct |
36 |
Correct |
156 ms |
58072 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
27 ms |
16728 KB |
Output is correct |
2 |
Correct |
25 ms |
16728 KB |
Output is correct |
3 |
Correct |
51 ms |
23924 KB |
Output is correct |
4 |
Correct |
45 ms |
22796 KB |
Output is correct |
5 |
Correct |
80 ms |
33200 KB |
Output is correct |
6 |
Correct |
79 ms |
32532 KB |
Output is correct |
7 |
Correct |
78 ms |
33108 KB |
Output is correct |
8 |
Correct |
99 ms |
33108 KB |
Output is correct |
9 |
Correct |
88 ms |
35036 KB |
Output is correct |
10 |
Correct |
64 ms |
26196 KB |
Output is correct |
11 |
Correct |
142 ms |
52052 KB |
Output is correct |
12 |
Correct |
1 ms |
344 KB |
Output is correct |
13 |
Correct |
1 ms |
600 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
344 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
21 ms |
16856 KB |
Output is correct |
19 |
Correct |
22 ms |
16732 KB |
Output is correct |
20 |
Correct |
20 ms |
16732 KB |
Output is correct |
21 |
Correct |
22 ms |
16884 KB |
Output is correct |
22 |
Correct |
128 ms |
37200 KB |
Output is correct |
23 |
Correct |
169 ms |
57036 KB |
Output is correct |
24 |
Correct |
155 ms |
58196 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
65 ms |
32928 KB |
Output is correct |
2 |
Correct |
74 ms |
37508 KB |
Output is correct |
3 |
Correct |
22 ms |
16732 KB |
Output is correct |
4 |
Correct |
24 ms |
16732 KB |
Output is correct |
5 |
Correct |
223 ms |
78348 KB |
Output is correct |
6 |
Correct |
267 ms |
83540 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
121 ms |
44316 KB |
Output is correct |
9 |
Correct |
134 ms |
51212 KB |
Output is correct |
10 |
Correct |
66 ms |
32964 KB |
Output is correct |
11 |
Correct |
74 ms |
37816 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
344 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
22 ms |
16740 KB |
Output is correct |
17 |
Correct |
23 ms |
16732 KB |
Output is correct |
18 |
Correct |
73 ms |
37424 KB |
Output is correct |
19 |
Correct |
86 ms |
42392 KB |
Output is correct |
20 |
Correct |
73 ms |
33844 KB |
Output is correct |
21 |
Correct |
72 ms |
33372 KB |
Output is correct |
22 |
Correct |
70 ms |
35020 KB |
Output is correct |
23 |
Correct |
76 ms |
38436 KB |
Output is correct |
24 |
Correct |
27 ms |
16728 KB |
Output is correct |
25 |
Correct |
25 ms |
16728 KB |
Output is correct |
26 |
Correct |
51 ms |
23924 KB |
Output is correct |
27 |
Correct |
45 ms |
22796 KB |
Output is correct |
28 |
Correct |
80 ms |
33200 KB |
Output is correct |
29 |
Correct |
79 ms |
32532 KB |
Output is correct |
30 |
Correct |
78 ms |
33108 KB |
Output is correct |
31 |
Correct |
99 ms |
33108 KB |
Output is correct |
32 |
Correct |
1 ms |
348 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
0 ms |
348 KB |
Output is correct |
35 |
Correct |
1 ms |
348 KB |
Output is correct |
36 |
Correct |
0 ms |
348 KB |
Output is correct |
37 |
Correct |
0 ms |
388 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 |
2 ms |
860 KB |
Output is correct |
42 |
Correct |
1 ms |
448 KB |
Output is correct |
43 |
Correct |
1 ms |
604 KB |
Output is correct |
44 |
Correct |
0 ms |
348 KB |
Output is correct |
45 |
Correct |
1 ms |
604 KB |
Output is correct |
46 |
Correct |
1 ms |
348 KB |
Output is correct |
47 |
Correct |
1 ms |
604 KB |
Output is correct |
48 |
Correct |
22 ms |
7004 KB |
Output is correct |
49 |
Correct |
21 ms |
8028 KB |
Output is correct |
50 |
Correct |
23 ms |
8032 KB |
Output is correct |
51 |
Correct |
20 ms |
7868 KB |
Output is correct |
52 |
Correct |
20 ms |
7772 KB |
Output is correct |
53 |
Correct |
42 ms |
14732 KB |
Output is correct |
54 |
Correct |
5 ms |
2136 KB |
Output is correct |
55 |
Correct |
15 ms |
5380 KB |
Output is correct |
56 |
Correct |
1 ms |
604 KB |
Output is correct |
57 |
Correct |
5 ms |
2140 KB |
Output is correct |
58 |
Correct |
3 ms |
1372 KB |
Output is correct |
59 |
Correct |
102 ms |
33936 KB |
Output is correct |
60 |
Correct |
146 ms |
45068 KB |
Output is correct |
61 |
Correct |
167 ms |
62052 KB |
Output is correct |
62 |
Correct |
167 ms |
61928 KB |
Output is correct |
63 |
Correct |
138 ms |
49236 KB |
Output is correct |
64 |
Correct |
166 ms |
62036 KB |
Output is correct |
65 |
Correct |
165 ms |
62056 KB |
Output is correct |
66 |
Correct |
59 ms |
22868 KB |
Output is correct |
67 |
Correct |
156 ms |
58072 KB |
Output is correct |
68 |
Correct |
88 ms |
35036 KB |
Output is correct |
69 |
Correct |
64 ms |
26196 KB |
Output is correct |
70 |
Correct |
142 ms |
52052 KB |
Output is correct |
71 |
Correct |
1 ms |
344 KB |
Output is correct |
72 |
Correct |
1 ms |
600 KB |
Output is correct |
73 |
Correct |
0 ms |
348 KB |
Output is correct |
74 |
Correct |
0 ms |
348 KB |
Output is correct |
75 |
Correct |
0 ms |
344 KB |
Output is correct |
76 |
Correct |
0 ms |
348 KB |
Output is correct |
77 |
Correct |
21 ms |
16856 KB |
Output is correct |
78 |
Correct |
22 ms |
16732 KB |
Output is correct |
79 |
Correct |
20 ms |
16732 KB |
Output is correct |
80 |
Correct |
22 ms |
16884 KB |
Output is correct |
81 |
Correct |
128 ms |
37200 KB |
Output is correct |
82 |
Correct |
169 ms |
57036 KB |
Output is correct |
83 |
Correct |
155 ms |
58196 KB |
Output is correct |
84 |
Correct |
199 ms |
69344 KB |
Output is correct |
85 |
Correct |
233 ms |
71480 KB |
Output is correct |
86 |
Correct |
239 ms |
80308 KB |
Output is correct |
87 |
Correct |
255 ms |
82260 KB |
Output is correct |
88 |
Correct |
0 ms |
344 KB |
Output is correct |
89 |
Correct |
247 ms |
82256 KB |
Output is correct |
90 |
Correct |
191 ms |
72016 KB |
Output is correct |
91 |
Correct |
171 ms |
65104 KB |
Output is correct |