Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- const int INF = 2e9;
- int main()
- {
- int n;
- cin >> n;
- vector<int> v(n);
- vector<int> dp1(n + 1), dp2(n + 1), dp3(n + 1), dp4(n + 1);
- for(int i = 0; i < n; i++) {
- cin >> v[i];
- }
- for(int i = 0; i < n; i++) {
- dp1[i] = -INF;
- dp2[i] = -INF;
- dp3[i] = -INF;
- dp4[i] = -INF;
- }
- for(int i = n - 1; i >= 0; i--) {
- dp1[i] = max(dp1[i + 1], v[i]);
- }
- for(int i = n - 2; i >= 0; i--) {
- dp2[i] = max(dp2[i + 1], dp1[i + 1] - v[i]);
- }
- for(int i = n - 3; i >= 0; i--) {
- dp3[i] = max(dp3[i + 1], dp2[i + 1] + v[i]);
- }
- for(int i = n - 4; i >= 0; i--) {
- dp4[i] = max(dp4[i + 1], dp3[i + 1] - v[i]);
- }
- int res = -INF;
- for(int i = 0; i <= n; i++) {
- res = max(res, dp4[i]);
- }
- cout << res << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement