<span style= "FONT-SIZE:18PX;" > #include <iostream> #include <cstdio> #include <climits> #include <cstring>using namespace Std;int dp[110][210],dis,t,n;int D[110],p[110];char str[100];void input () {gets (str); n=0; d[0]=0; SSCANF (str, "%d", &dis); while (gets (str)!=null) {if (str[0]== ') return; ++n; SSCANF (str, "%d%d", &d[n],&p[n]); if (D[n]>dis) n--; }}void solve () {for (Int. i=0;i<=n;i++) for (int j=0;j<=200;j++) Dp[i][j]=int_max; dp[0][100]=0; for (int i=1;i<=n;i++) {int w=d[i]-d[i-1]; for (int j=0;j+w<=200;j++) if (Dp[i-1][j+w]!=int_max) dp[i][j]=dp[i-1][j+w]; for (int j=0;j<=200;j++) for (int k=0;k<=j;k++) {if (j-k+w<=200&&dp[i-1][j-k +w]!=int_max) {dp[i][j]=min (dp[i][j],dp[i-1][j-k+w]+k*p[i]); } } } if (dis-d[n]>100| | Dp[n][100+dis-d[n]]==int_max) printf ("impossible\n"); else printf ("%d\n", Dp[n][100+dis-d[n]]);} int main () {int t; Gets (str); SSCANF (str, "%d", &t); Gets (str); while (t--) {input (); Solve (); if (t) printf ("\ n"); } return 0;} </span>
Uva--uva 10201Adventures in Moving-part IV