Advertisement
BaoJIaoPisu

Untitled

Oct 22nd, 2021
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.74 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. #define fi first
  3. #define se second
  4. #define pb push_back
  5. #define pf push_front
  6. #define popb pop_back
  7. #define popf pop_front
  8. #define ins insert
  9. #define pq priority_queue
  10. #define minEle min_element
  11. #define maxEle max_element
  12. #define lb lower_bound //first pos >= val
  13. #define ub upper_bound // first pos > val
  14. #define cnt_bit __builtin_popcount
  15. #define debug(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
  16. //#pragma GCC optimize("Ofast")
  17. //#pragma GCC target("avx,avx2,fma")
  18. using namespace std;
  19.  
  20. typedef long long ll;
  21. typedef pair<ll, ll> pll;
  22. typedef pair<int, int> pii;
  23.  
  24.  
  25. int d4x[4] = {1, 0, -1, 0}; int d4y[4] = {0, 1, 0, -1};
  26. int d8x[8] = {0, 1, 1, 1, 0, -1, -1, -1};
  27. int d8y[8] = {1, 1, 0, -1, -1, -1, 0, 1};
  28.  
  29. const ll oo = 1e18;
  30. const ll maxN = 1e6;
  31.  
  32. /* Author : Le Ngoc Bao Anh, 10A5, LQD High School for Gifted Student */
  33.  
  34. void maximize(int &a, int b) {
  35.     a = max(a, b);
  36. }
  37.  
  38. void minimize(int &a, int b) {
  39.     a = min(a, b);
  40. }
  41.  
  42. ll dp[500][500], pref[maxN], a[maxN];
  43.  
  44. ll DP(int L, int R) {
  45.     if(dp[L][R]) return dp[L][R];
  46.     if(L == R) return 0;
  47.     if(L + 1 == R) return a[L] + a[R];
  48.     dp[L][R] = oo;
  49.    
  50.     for(int mid = L; mid <= R; mid++) {
  51.         dp[L][R] = min(dp[L][R], DP(L, mid) + DP(mid + 1, R) + (pref[mid] - pref[L - 1]) + (pref[R] - pref[mid]));
  52.     }
  53.    
  54.     return dp[L][R];
  55. }
  56.  
  57. void solve() {
  58.     int n;
  59.     cin >> n;
  60.     for(int i = 1; i <= n; i++) {
  61.         cin >> a[i];
  62.         pref[i] = pref[i - 1] + a[i];
  63.     }
  64.    
  65.     cout << DP(1, n);
  66. }
  67.  
  68. int main()
  69. {
  70.     ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  71.     //freopen("input.inp", "r", stdin);
  72.  
  73. //    ll tc, ddd = 0;
  74. //    cin >> tc;
  75. //    while(tc--) {
  76.         //ddd++;
  77.         //cout << "Case #" << ddd << ": ";
  78.         solve();
  79. //    }
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement