Advertisement
DASBD72

143

Apr 26th, 2019
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.78 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<cmath>
  4. #define MAXN 2147483647
  5. using namespace std;
  6. int N, a[1005] = {};
  7. int awe[1005][1005] = {};
  8. int dp(int L,int R){
  9.     if(L == R)return a[L];
  10.     if(awe[L][R] != 0)return awe[L][R];
  11.     int tmp = MAXN, tmpL, tmpR, count = 0;
  12.    
  13.     for(int mid = L;mid<R;mid++){
  14.         count = 0;
  15.         for(int i = L;i <= R;i++)count+=a[i];
  16.         tmpL = dp(L, mid), tmpR = dp(mid+1, R);
  17.         if(L!=mid)count+=tmpL;
  18.         if(mid+1!=R)count+=tmpR;
  19.         tmp = count < tmp ? count : tmp;
  20.     }
  21.     awe[L][R] = tmp;
  22.     return tmp;
  23. }
  24. int main(){
  25.     int T,ans;
  26.     for(scanf("%d",&T);T;T--){
  27.         memset(awe,0,sizeof(awe));
  28.         scanf("%d",&N);
  29.         for(int iN = 1;iN <= N;iN++){
  30.             scanf("%d",&a[iN]);
  31.         }
  32.         for(int i=1;i<N;i++)awe[i][i+1]=a[i]+a[i+1];
  33.         ans=dp(1,N);
  34.         printf("%d\n",ans);
  35.     }
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement