#include <bits/stdc++.h>
using namespace std;
long long calc(vector<long long>& 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) {
long long 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], maxVal - minVal);
}
return dp.back();
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n, q;
cin >> n >> q;
vector<long long> 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<long long int>&)':
Main.cpp:8:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long 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 |
7 ms |
312 KB |
Output is correct |
2 |
Correct |
7 ms |
316 KB |
Output is correct |
3 |
Correct |
7 ms |
204 KB |
Output is correct |
4 |
Correct |
7 ms |
316 KB |
Output is correct |
5 |
Correct |
7 ms |
316 KB |
Output is correct |
6 |
Correct |
8 ms |
316 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
312 KB |
Output is correct |
2 |
Correct |
7 ms |
316 KB |
Output is correct |
3 |
Correct |
7 ms |
204 KB |
Output is correct |
4 |
Correct |
7 ms |
316 KB |
Output is correct |
5 |
Correct |
7 ms |
316 KB |
Output is correct |
6 |
Correct |
8 ms |
316 KB |
Output is correct |
7 |
Execution timed out |
2055 ms |
440 KB |
Time limit exceeded |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
312 KB |
Output is correct |
2 |
Correct |
7 ms |
316 KB |
Output is correct |
3 |
Correct |
7 ms |
204 KB |
Output is correct |
4 |
Correct |
7 ms |
316 KB |
Output is correct |
5 |
Correct |
7 ms |
316 KB |
Output is correct |
6 |
Correct |
8 ms |
316 KB |
Output is correct |
7 |
Execution timed out |
2055 ms |
440 KB |
Time limit exceeded |
8 |
Halted |
0 ms |
0 KB |
- |