//
紫书上的第一道dp, 水平不够, 看了好多遍, 还是没理解精神, 说说借鉴进步之处
1. 逆推, 假设能到, 看dp【0】【1】
2.动态方程用的时间+站
3.初始化全部,紫书初始化了一部分, 没看懂
//
#include<iostream>
#include<cstdio>
#include<string.h>
#include<algorithm>
using namespace std;
const int INF = (1<<30) - 1;
const int maxt = 200 + 5;
const int maxn = 50 + 5;
int dp[maxt][maxn];
int has_train[maxt][maxn][2];
int t[maxt];
int main()
{
int T, N, M1, M2, d;
int kase = 0;
while(cin >> N && N)
{
memset(dp, 0, sizeof(dp));
memset(has_train, 0, sizeof(has_train));
memset(t, 0, sizeof(t));
cin >> T;
for(int i = 1; i < N; ++i) cin >> t[i];
cin >> M1;
while(M1--)
{
cin >> d;
for(int i = 1; i < N; ++i)
{
if(d<=T) has_train[d][i][0] = 1;
d += t[i];
}
}
cin >> M2;
while(M2--)
{
cin >> d;
for(int i = N; i > 1; --i)
{
if(d<=T) has_train[d][i][1] = 1;
d += t[i-1];
}
}
for(int i = 0; i < maxt; ++i)
for(int j = 0; j < maxn; ++j) dp[i][j] = INF;
dp[T][N] = 0;
for(int i = T - 1; i >= 0; --i)
{
for(int j = 1; j <= N; ++j)
{
dp[i][j] = dp[i+1][j] + 1;
if(j < N && has_train[i][j][0] && i + t[j] <= T)
dp[i][j] = min(dp[i][j], dp[i+t[j]][j+1]);
if(j > 1 && has_train[i][j][1] && i + t[j-1] <= T)
dp[i][j] = min(dp[i][j], dp[i+t[j-1]][j-1]);
}
}
cout<<"Case Number "<<++kase<< ": ";
if(dp[0][1]>=INF) cout<<"impossible\n";
else cout<<dp[0][1]<<"\n";
}
return 0;
}