Advertisement
Guest User

Untitled

a guest
Nov 12th, 2019
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.20 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define pii pair<int, int>
  3. #define vi vector<int>
  4. #define ll long long
  5. #define pb push_back
  6. #define f first
  7. #define s second
  8. using namespace std;
  9.  
  10. int dp(int i, int n, vi &val, vi &tabela) {
  11.   if(i == 0)
  12.     return tabela[i] = val[0];
  13.  
  14.   if(tabela[i] != (int)-1e9)
  15.     return tabela[i];
  16.  
  17.   return tabela[i] = max(val[i],dp(i-1, n, val, tabela)+val[i]);
  18. }
  19.  
  20. int main() {
  21.   int n;
  22.   scanf("%d", &n);
  23.   vi tabela1(n, -1e9);
  24.   vi somaorig(n);
  25.   vi soma(n);
  26.   vi maximos(n);
  27.   vi val(n);
  28.  
  29.   for(int i = 0; i < n; i++) {
  30.     scanf("%d", &val[i]);
  31.   }
  32.  
  33.   somaorig[0] = val[0];
  34.   for(int i = 1; i < n; i++) {
  35.     somaorig[i] = somaorig[i-1] + val[i];
  36.   }
  37.  
  38.   dp(n-1, n, val, tabela1);
  39.  
  40.   soma[n-1] = val[n-1];
  41.   for(int i = n-2; i >= 0; i--) {
  42.     soma[i] = val[i] + soma[i+1];
  43.   }
  44.   maximos[n-1] = val[n-1];
  45.   int maior = maximos[n-1];
  46.   for(int i = n-2; i >= 0; i--) {
  47.     maior = max(soma[i], maior);
  48.     maximos[i] = maior;
  49.   }
  50.  
  51.   maior = tabela1[n-1];
  52.   for(int i = n-2; i >= 0; i--) {
  53.     maior = max(maior, max(tabela1[i], somaorig[i] + maximos[i+1]));
  54.   }
  55.  
  56.   if(maior >= 0)
  57.     cout << maior << endl;
  58.   else
  59.     cout << "0" << endl;
  60.   return 0;
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement