[Cpp]
Description: I represents the gas stations, j Represents the remaining fuel at the gas station I, and k represents the fuel to be added at the gas station I.
# Include <cstdio>
# Include <cstdlib>
# Include <cstring>
Int v [110] [210], arr [110] [2];
Int min (int x, int y)
{
Return x> y? Y: x;
}
Int main ()
{
// Freopen ("a.txt", "r", stdin );
Int t, n, len, sum, c;
Char str [110];
Scanf ("% d", & t );
While (t --)
{
Scanf ("% d", & n );
Len = 1;
Getchar ();
While (gets (str) & str [0]! = '\ 0 ')
{
Sscanf (str, "% d", & arr [len] [0], & arr [len] [1]);
If (arr [len] [0] <= n) len ++;
}
Memset (v, 0x7f, sizeof (v ));
V [0] [100] = arr [0] [0] = 0;
For (int I = 1; I <len; I ++)
For (int j = 0; j <= 200; j ++)
{
C = arr [I] [0]-arr [I-1] [0];
If (j-c> = 0) for (int k = 0; j + k-c <= 200; k ++)
V [I] [j + k-c] = min (v [I] [j + k-c], v [I-1] [j] + k * arr [I] [1]);
}
Arr [0] [1] = sum = v [0] [0];
C = n-arr [len-1] [0];
For (int I = 100; I <= 200; I ++)
{
If (arr [len-1] [1]! = V [0] [0] & sum> arr [len-1] [1] * c + v [len-1] [I] & I + c <= 200)
Sum = arr [len-1] [1] * c + v [len-1] [I];
}
If (sum = v [0] [0]) printf ("Impossible \ n ");
Else printf ("% d \ n", sum );
If (t) puts ("");
}
Return 0;
}