Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <climits>
- using namespace std;
- int maximizeExpression(int A[], int n)
- {
- int first[n + 1], second[n], third[n - 1], fourth[n - 2];
- for (int i = 0; i <= n - 3; i++) {
- first[i] = second[i] = third[i] = fourth[i] = INT_MIN;
- }
- first[n - 2] = second[n - 2] = third[n - 2] = INT_MIN;
- first[n - 1] = second[n - 1] = first[n] = INT_MIN;
- for (int i = n - 1; i >= 0; i--) {
- first[i] = max(first[i + 1], A[i]);
- }
- for (int i = n - 2; i >= 0; i--) {
- second[i] = max(second[i + 1], first[i + 1] - A[i]);
- }
- for (int i = n - 3; i >= 0; i--) {
- third[i] = max(third[i + 1], second[i + 1] + A[i]);
- }
- for (int i = n - 4; i >= 0; i--) {
- fourth[i] = max(fourth[i + 1], third[i + 1] - A[i]);
- }
- return fourth[0];
- }
- int main()
- {
- int n;
- cin>>n;
- int arr[n];
- for(int i=0;i<n;i++){
- cin>>arr[i];
- }
- if (n >= 4) {
- cout << maximizeExpression(arr, n);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment