pipioj 1019: 堆石子(区间dp)
http://www.pipioj.online/problem.php?id=1019
1 #define IO std::ios::sync_with_stdio(0); 2 #define bug(x) cout<<#x<<" is "<<x<<endl 3 #include <bits/stdc++.h> 4 #define iter ::iterator 5 using namespace std; 6 typedef long long ll; 7 typedef pair<int,int>P; 8 #define pb push_back 9 #define mk make_pair 10 #define se second 11 #define fi first 12 #define rs o*2+1 13 #define ls o*2 14 const ll mod=1e9+7; 15 const int N=1e3+5,M=3e6; 16 17 18 int d[N][N],a[N]; 19 20 int n; 21 int main(){ 22 23 while(~scanf("%d",&n)){ 24 for(int i=1;i<=2*n-1;i++){ 25 for(int j=i+1;j<=2*n;j++)d[i][j]=1e9; 26 } 27 for(int i=1;i<=n;i++){ 28 scanf("%d",&a[i]); 29 a[i+n]=a[i]; 30 } 31 for(int i=1;i<=2*n;i++){ 32 a[i]+=a[i-1]; 33 } 34 for(int l=n;l>=1;l--){ 35 for(int r=l+1;r<=n;r++){ 36 for(int k=l;k<r;k++){ 37 d[l][r]=min(d[l][r],d[l][k]+d[k+1][r]+a[r]-a[l-1]); 38 } 39 } 40 } 41 int ans=d[1][n]; 42 printf("%d\n",ans); 43 } 44 45 }