#include <bits/stdc++.h>
#define pb push_back
#define s second
#define f first
#define ll long long
#define int ll
using namespace std;
const int N = 3005;
ll a[N],dp[4][4][N];
pair <int,int> d[4][4][N];
main (){
ios_base::sync_with_stdio(0),cin.tie(NULL),cout.tie(NULL);
int n,m;
cin>>n>>m;
for (int i=1;i<=n;i++)
cin>>a[i];
while(m--){
int l,r,k;
cin>>l>>r>>k;
for (int i=l;i<=r;i++) a[i] += k;
for (int i=1;i<=n;i++){
d[0][0][i] = d[0][1][i] = d[1][0][i] = d[1][1][i] = {1e18,-1e18};
dp[0][1][i] = dp[1][1][i] = dp[0][0][i] = dp[1][0][i] = 0;
}
d[1][1][1] = {a[1],a[1]};
d[1][0][1] = {a[1],a[1]};
for (int i=2;i<=n;i++){
//0 1
int x = dp[0][0][i-1] + max(d[0][0][i-1].s,a[i]) - min(d[0][0][i-1].f,a[i]);
int y = dp[1][0][i-1] + max(d[1][0][i-1].s,a[i]) - min(d[1][0][i-1].f,a[i]);
if (x > y) dp[0][1][i] = x,d[0][1][i] = {min(d[0][0][i-1].f,a[i]),max(d[0][0][i-1].s,a[i])};
else dp[0][1][i] = y,d[0][1][i] = {min(d[1][0][i-1].f,a[i]),max(d[1][0][i-1].s,a[i])};
//1 1
dp[1][1][i] = max(dp[0][1][i-1],dp[1][1][i-1]);
//1 0
dp[1][0][i] = max(dp[1][1][i-1],dp[0][1][i-1]);
d[1][0][i] = {a[i],a[i]};
//0 0
x = dp[1][0][i-1]; y = dp[0][0][i-1];
dp[0][0][i] = max(x,y);
if (x>y) d[0][0][i] = {min(d[1][0][i-1].f,a[i]),max(d[1][0][i-1].s,a[i])};
else d[0][0][i] = {min(d[0][0][i-1].f,a[i]),max(d[0][0][i-1].s,a[i])};
}
cout<<max(dp[1][1][n],dp[0][1][n])<<"\n";
}
}
Compilation message
Main.cpp:11:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
11 | main (){
| ^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |