#include <bits/stdc++.h>
using namespace std;
long long calc(vector<int>& a) {
vector<long long> dp(a.size(), 0); // max val if ends in i
dp[0] = 0;
for (int i = 1; i < a.size(); ++i) {
int maxVal = a[i], minVal = a[i];
for (int j = i - 1; j >= 0; --j) {
dp[i] = max(dp[i], dp[j] + maxVal - minVal);
maxVal = max(maxVal, a[j]);
minVal = min(minVal, a[j]);
}
dp[i] = max(dp[i], (long long)maxVal - minVal);
}
return dp.back();
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n, q;
cin >> n >> q;
vector<int> a(n);
for (auto& i : a) {
cin >> i;
}
while (q--) {
int l, r, x;
cin >> l >> r >> x;
--l, --r;
for (int i = l; i <= r; ++i) {
a[i] += x;
}
cout << calc(a) << '\n';
}
return 0;
}
Compilation message
Main.cpp: In function 'long long int calc(std::vector<int>&)':
Main.cpp:8:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
8 | for (int i = 1; i < a.size(); ++i) {
| ~~^~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
204 KB |
Output is correct |
2 |
Correct |
9 ms |
324 KB |
Output is correct |
3 |
Correct |
10 ms |
204 KB |
Output is correct |
4 |
Correct |
10 ms |
332 KB |
Output is correct |
5 |
Correct |
11 ms |
324 KB |
Output is correct |
6 |
Incorrect |
9 ms |
320 KB |
Output isn't correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
204 KB |
Output is correct |
2 |
Correct |
9 ms |
324 KB |
Output is correct |
3 |
Correct |
10 ms |
204 KB |
Output is correct |
4 |
Correct |
10 ms |
332 KB |
Output is correct |
5 |
Correct |
11 ms |
324 KB |
Output is correct |
6 |
Incorrect |
9 ms |
320 KB |
Output isn't correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
204 KB |
Output is correct |
2 |
Correct |
9 ms |
324 KB |
Output is correct |
3 |
Correct |
10 ms |
204 KB |
Output is correct |
4 |
Correct |
10 ms |
332 KB |
Output is correct |
5 |
Correct |
11 ms |
324 KB |
Output is correct |
6 |
Incorrect |
9 ms |
320 KB |
Output isn't correct |