水题记忆化dp
#include <iostream> #include <cstdio> #include <cstring> #define ll long long using namespace std; int t,n,k; ll a[1010]; ll dp[1100][1100]; ll DP(int x,int y){ if(x+1==y) return 2*min(a[x],a[y]); if(x>=y) return 0; if(dp[x][y]!=-1) return dp[x][y]; ll ret=1e17; for(int i=x;i<=y;i++){ ret=min(ret,a[i]*(y-x+1)+DP(x,i-1)+DP(i+1,y)); } return dp[x][y]=ret; } int main(){ scanf("%d",&t); for(int ca=1;ca<=t;ca++){ scanf("%d%d",&n,&k); for(int i=1;i<=n;i++) a[i]=k+i; memset(dp,-1,sizeof dp); printf("Case %d: %lld\n",ca,DP(1,n)); } return 0; }