Advertisement
facug91

Untitled

Feb 12th, 2016
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.61 KB | None | 0 0
  1. /*
  2.     By: facug91
  3.     From: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=771&page=show_problem&problem=1832
  4.     Name:
  5.     Date: 12/02/2016
  6. */
  7.  
  8. #include <bits/stdc++.h>
  9. #define EPS 1e-9
  10. #define MP make_pair
  11. #define F first
  12. #define S second
  13. #define endl "\n"
  14. #define DB(x) cerr << "#" << (#x) << ": " << (x) << " "
  15. #define DBL(x) cerr << "#" << (#x) << ": " << (x) << endl
  16. const double PI = 2.0*acos(0.0);
  17.  
  18. #define INF 1000000000
  19. //#define MOD 1000000007ll
  20. //#define MAXN 10000100
  21.  
  22. using namespace std;
  23. typedef long long ll;
  24. typedef pair<int, int> ii; typedef pair<int, ii> iii;
  25. typedef vector<int> vi; typedef vector<ii> vii;        typedef vector<iii> viii;
  26.  
  27. int n, a[105], rsq[105];
  28. ii DP[105][105];
  29.  
  30. ii dp (int l, int r) {
  31.     if (DP[l][r].F != -1) return DP[l][r];
  32.     if (l == r) return ii(0, a[l]);
  33.     ii ans(0, 0), aux;
  34.     for (int i=l+1; i<=r; i++) {
  35.         aux = dp(i, r);
  36.         aux.F += (rsq[i-1]-rsq[l-1]);
  37.         if (ans.F < aux.F) ans = aux;
  38.         aux = dp(l, i);
  39.         aux.F += (rsq[r]-rsq[i]);
  40.         if (ans.F < aux.F) ans = aux;
  41.     }
  42.     return DP[l][r] = ans;
  43. }
  44.  
  45. int main () {
  46.     #ifdef ONLINE_JUDGE
  47.         ios_base::sync_with_stdio(0); cin.tie(0);
  48.     #endif
  49.     //cout<<fixed<<setprecision(4); cerr<<fixed<<setprecision(4); //cin.ignore(INT_MAX, ' '); //cout << setfill('0') << setw(5) << 25
  50.     int tc = 1, i, j;
  51.    
  52.     while (cin>>n, n) {
  53.         for (i=1; i<=n; i++) cin>>a[i];
  54.         rsq[0] = 0;
  55.         for (i=2; i<=n; i++) rsq[i] = a[i] + rsq[i-1];
  56.         for (i=1; i<=n; i++) for (j=1; j<=n; j++) DP[i][j] = ii(-1, -1);
  57.         cout<<dp(0, n-1).F<<endl;
  58.     }
  59.    
  60.    
  61.    
  62.    
  63.    
  64.    
  65.    
  66.    
  67.    
  68.    
  69.    
  70.    
  71.    
  72.    
  73.    
  74.    
  75.    
  76.    
  77.    
  78.    
  79.     return 0;
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement