#include #include #include #include #include using namespace std; const int N = 505; const int M = 5050; const int C = 50000; int n,m,r,p,q; int point[N],to[M],next[M],flow[M],fei[M],cc; int dui[N],minz[N],pre[N],top,tail; bool indui[N],ever[N]; int xu[N]; int s,t; int getint() { int res=0; char ch=getchar(); while(ch<'0' || ch>'9') ch=getchar(); while('0'<=ch && ch<='9') { res=res*10+ch-'0'; ch=getchar(); } return res; } void Add(int x,int y,int z,int w) { cc++; next[cc]=point[x]; point[x]=cc; to[cc]=y; flow[cc]=z; fei[cc]=w; } void AddEdge(int x,int y,int z,int w) { Add(x,y,z,w); Add(y,x,0,-w); } bool Spfa() { memset(ever,0,sizeof ever); memset(minz,0x3f,sizeof minz); minz[s]=0; dui[1]=s; top=0;tail=1; indui[s]=1; ever[s]=1; while(top!=tail) { top++; if(top==N) top=0; int now=dui[top]; int then=point[now]; indui[now]=0; while(then) { if(flow[then] && minz[to[then]]>minz[now]+fei[then]) { minz[to[then]]=minz[now]+fei[then]; pre[to[then]]=then; if(!indui[to[then]]) { indui[to[then]]=1; ever[to[then]]=1; tail++; if(tail==N) tail=0; dui[tail]=to[then]; } } then=next[then]; } } if(ever[t]) return 1; return 0; } void Zeng(int &x,int &y) { int now; int val=C; now=t; while(now!=s) { val=min(val,flow[pre[now]]); now=to[pre[now]^1]; } x-=val; y+=val*minz[t]; now=t; while(now!=s) { flow[pre[now]]-=val; flow[pre[now]^1]+=val; now=to[pre[now]^1]; } } void f() { int i,j; m=getint(); for(i=1;i<=n;i++) xu[i]=getint(); r=getint(); p=getint(); q=getint(); memset(point,0,sizeof point); cc=1; s=2*n+1; t=s+1; for(i=1;i<=n;i++) AddEdge(s,i,xu[i],0); for(i=1;i<=n;i++) AddEdge(i+n,t,xu[i],0); for(i=2;i<=n;i++) AddEdge(i-1+n,i+n,C,0); AddEdge(s,n+1,m,0); if(p<1) p=1; if(p<=n) AddEdge(s,n+p,C,q); for(i=1;i<=r;i++) { int a=getint(); int b=getint(); if(b<1) continue; for(j=1;j+b<=n;j++) AddEdge(j,j+b+n,C,a); } int need=0; for(i=1;i<=n;i++) need+=xu[i]; int ans=0; while(Spfa()) Zeng(need,ans); if(need<=0) printf("%d\n",ans); else printf("No solution\n"); } int main() { int T=getint(); while((scanf("%d",&n))!=EOF) f(); return 0; }