This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define int long long
#define fastio ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
using namespace std;
const int N = 2e5+5;
int dp[N][2]; //The maximum sum when we get to the ith element and is ascend/descending
signed main(){
fastio
int n,q;
cin >> n >> q;
int arr[n+1];
for(int i = 1;i <= n;i++){
cin >> arr[i];
}
while(q--){
int l,r,x;
cin >> l >> r >> x;
for(int i = l;i <= r;i++) arr[i] += x;
dp[1][0] = dp[1][1] = 0;
for(int i = 2;i <= n;i++){
if(arr[i]>=arr[i-1]){
dp[i][0] = max(dp[i-1][0]+arr[i]-arr[i-1],dp[i-1][1]);
dp[i][1] = max(dp[i-1][0],dp[i-1][1]);
}else if(arr[i] < arr[i-1]){
dp[i][0] = max(dp[i-1][0],dp[i-1][1]);
dp[i][1] = max(dp[i-1][0],dp[i-1][1]+arr[i-1]-arr[i]);
}
}
cout << max(dp[n][0],dp[n][1]) << "\n";
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |