#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int INF=10000;
int dp[205][205];
int has_train[205][205][2];
int main()
{
int n,t;
int kase=0;
while(cin>>n&&n&&cin>>t)
{
int sch[205];
for(int i=1;i<=n-1;i++) cin>>sch[i];
memset(has_train,0,sizeof(has_train));
int m1,m2,d;
cin>>m1;
while(m1--){
cin>>d;
for(int j=1;j<=n-1;j++){
if(d<=t) has_train[d][j][0]=1;
d+=sch[j];
}
}
cin>>m2;
while(m2--){
cin>>d;
for(int j=n-1;j>=1;j--){
if(d<=t) has_train[d][j+1][1]=1;
d+=sch[j];
}
}
for(int i=1;i<=n-1;i++) dp[t][i]=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+sch[j]<=t)
dp[i][j]=min(dp[i][j],dp[i+sch[j]][j+1]);
if(j>1&&has_train[i][j][1]&&i+sch[j-1]<=t)
dp[i][j]=min(dp[i][j],dp[i+sch[j-1]][j-1]);
}
}
cout<<"Case Number "<<++kase<<": ";
if(dp[0][1]>=INF) cout<<"impossible"<<endl;
else cout<<dp[0][1]<<endl;
}
return 0;
}
思路:往回走,看目前的点能否往下走,dp[i]=min(dp[i][j],左右开的列车},d[0][1]就是到d[t][n]的等待时间。