注意动态规划的思路 就是由各个状态递推到下一个状态
规定dp【i】【j】为 第i跟数分割为j块
#include <bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
int dp[2200][22];
int sum[2200];
int a[2200];
int main()
{int n,d;while(~scanf("%d%d", &n, &d)){memset(dp,0,sizeof(dp));memset(sum,0,sizeof(sum));memset(a,0,sizeof(a));for(int i = 1; i <= n; i++){scanf("%d", &a[i]);sum[i]=sum[i-1]+a[i];}for(int i = 1; i <= n; i++){for(int j = 1; j <= d; j++)dp[i][j] = INF;}for(int i = 1; i <= n; i++)if(sum[i]%10 >= 5)dp[i][0] = sum[i]/10*10+10;elsedp[i][0] = sum[i]/10*10;for(int i = 1; i <= d; i++){dp[0][i] = 0;}for(int i = 1; i <= n; i++){for(int j = 1; j <= d; j++){for(int k = 1; k < i; k++)//有前面的状态递推过来{int to = sum[i] - sum[k];if(to % 10 >= 5){ to = to/10*10+10;dp[i][j] = min(dp[i][j], dp[k][j-1]+to);}else{to = to/10*10;dp[i][j] = min(dp[i][j], dp[k][j-1] + to);}}}}int ans = INF;for(int i = 0; i <= d; i++){ans = min(ans, dp[n][i]);}printf("%d\n",ans);}
}