Cow can get m money at the end of each month, there are a total of n problems to solve
If you solve a problem, you must pay a deposit in the first month, and pay the end payment in i+1 month, need to solve the problem in order, the monthly money can only be used in the next month, cannot accumulate
F[i][j]=max (M-∑b[j]-∑b[k]) {where k should meet f[i-1][k]>=∑a[j]-∑a[k]}
F[I][J] Indicates the maximum amount of money that can be left for a J question before the first month of settlement
#include "stdio.h" #include "string.h" int dp[2010][310];int Max (int a,int b) {if (a<b) return B; else return A;} int main () {int i,j,ans,m,n,a,b; int suma[310],sumb[310]; while (scanf ("%d%d", &m,&n)!=eof) {memset (suma,0,sizeof (Suma)); memset (sumb,0,sizeof (sumb)); for (i=1;i<=n;i++) {scanf ("%d%d", &a,&b); Suma[i]=suma[i-1]+a; Sumb[i]=sumb[i-1]+b; } ans=0; Memset (Dp,-1,sizeof (DP)); Dp[1][0]=m; while (1) {ans++; for (i=1;i<=n;i++) for (j=i;j>=0;j--) {if (dp[ans-1][j]>=suma[i ]-SUMA[J] && m>=sumb[i]-sumb[j]) Dp[ans][i]=max (Dp[ans][i],m-sumb[i]+sumb[j]); if (dp[ans][i]==m) break; } if (dp[ans][n]>=0) break; } printf ("%d\n", ans+1); } return 0;}
POJ 3265 DP