#include<bits/stdc++.h>
using namespace std;
using i64 = long long;
const int MAXN = 3e3 + 5;
const i64 oo64 = 1e18 + 5;
struct event {
int l, r, x;
event(int l = 0, int r = 0, int x = 0) : l(l), r(r), x(x) {}
};
const int INC = 0;
const int DEC = 1;
int N, Q;
i64 a[MAXN];
event query[MAXN];
i64 dp[MAXN][2];
void solve() {
memset(dp, -0x3f, sizeof dp);
dp[0][INC] = dp[0][DEC] = 0;
dp[1][INC] = dp[1][DEC] = 0;
for(int i = 2; i <= N; i++) {
dp[i][INC] = dp[i][DEC] = max(dp[i - 1][INC], dp[i - 1][DEC]);
if(a[i - 1] < a[i]) {
dp[i][INC] = max(dp[i][INC], max(dp[i - 1][INC], dp[i - 2][DEC]) + a[i] - a[i - 1]);
} else if(a[i - 1] > a[i]) {
dp[i][DEC] = max(dp[i][DEC], max(dp[i - 1][DEC], dp[i - 2][INC]) + a[i - 1] - a[i]);
}
}
cout << max(dp[N][INC], dp[N][DEC]) << "\n";
}
signed main() {
if(fopen(".inp", "r")) {
freopen(".inp", "r", stdin);
freopen(".out", "w", stdout);
}
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> N >> Q;
for(int i = 1; i <= N; i++) cin >> a[i];
for(int i = 1; i <= Q; i++) {
auto &[l, r, x] = query[i];
cin >> l >> r >> x;
}
for(int i = 1; i <= Q; i++) {
const auto [l, r, x] = query[i];
for(int j = l; j <= r; j++) a[j] += x;
solve();
}
}
Compilation message
Main.cpp: In function 'int main()':
Main.cpp:40:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
40 | freopen(".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~
Main.cpp:41:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
41 | freopen(".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
44 ms |
660 KB |
Output is correct |
8 |
Correct |
45 ms |
668 KB |
Output is correct |
9 |
Correct |
46 ms |
660 KB |
Output is correct |
10 |
Correct |
46 ms |
660 KB |
Output is correct |
11 |
Correct |
45 ms |
604 KB |
Output is correct |
12 |
Correct |
46 ms |
604 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
44 ms |
660 KB |
Output is correct |
8 |
Correct |
45 ms |
668 KB |
Output is correct |
9 |
Correct |
46 ms |
660 KB |
Output is correct |
10 |
Correct |
46 ms |
660 KB |
Output is correct |
11 |
Correct |
45 ms |
604 KB |
Output is correct |
12 |
Correct |
46 ms |
604 KB |
Output is correct |
13 |
Incorrect |
2 ms |
860 KB |
Output isn't correct |
14 |
Halted |
0 ms |
0 KB |
- |