#include <bits/stdc++.h> using namespace std; #define fru(j,n) for(int j=0; j<(n); ++j) #define tr(it,v) for(auto it=(v).begin(); it!=(v).end(); ++it) #define x first #define y second #define pb push_back #define mp make_pair #define ALL(G) (G).begin(),(G).end() typedef long long ll; typedef double D; typedef pair<int,int> pii; typedef vector<int> vi; const int inft = 1000000009; const int MAXN = 1000006,PT=41; const ll bound=1LL<<PT; int A[MAXN],K[PT+1]; pii DP[PT+1][MAXN]; char str[MAXN]; int n,m,k,u; int S[MAXN]; pii W[MAXN]; ll step[MAXN]; bool never[MAXN]; inline int ADD(int a,int b){ if(a+b>=m)return a+b-m; return a+b; } void solve() { scanf("%d",&n); fru(i,n)scanf("%d",A+i); scanf("%d",&m); k=n%m; scanf("%s",str); fru(i,m){ u=str[i]=='W'?1:-1; DP[0][i]=pii(u,u); } K[0]=k; fru(i,PT+1)if(i)K[i]=ADD(K[i-1],K[i-1]); fru(i,PT+1)if(i){ fru(j,m){ u=ADD(j,K[i-1]); DP[i][j].x=DP[i-1][j].x+ DP[i-1][u].x; DP[i][j].y=min(DP[i-1][j].y,DP[i-1][j].x+ DP[i-1][u].y); if(DP[i][j].x>inft)DP[i][j].x=inft; if(DP[i][j].x<-inft)DP[i][j].x=-inft; if(DP[i][j].y<-inft)DP[i][j].y=-inft; } } ll ans=bound*n; fru(i,n){ step[i]=0;W[i]=pii(0,0); S[i]=i%m; never[i]=0; } for(int j=PT;j>=0;j--)fru(i,n)if(!never[i]){ pii temp; temp.x=W[i].x+DP[j][S[i]].x; temp.y=min(W[i].y,W[i].x+ DP[j][S[i]].y); // printf("s= %d temp %d, %d, w %d %d\n",s,temp.x,temp.y,w.x,w.y); if(temp.y+A[i]<=0){ continue; } else { if(j==PT){never[i]=true;} W[i]=temp; if(W[i].x>inft)W[i].x=inft; if(W[i].x<-inft)W[i].x=-inft; S[i]=ADD(S[i],K[j]); step[i]+=1LL<<(j); } } fru(i,n)if(!never[i])ans=min(ans,1LL+i+(step[i])*n); if(ans==bound*n)puts("-1"); else printf("%lld\n",ans); } int main() { // freopen("input.in", "r", stdin); // freopen("output.out", "w", stdout); int t=1; // scanf("%d",&t); fru(i,t) solve(); return 0; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 | #include <bits/stdc++.h> using namespace std; #define fru(j,n) for(int j=0; j<(n); ++j) #define tr(it,v) for(auto it=(v).begin(); it!=(v).end(); ++it) #define x first #define y second #define pb push_back #define mp make_pair #define ALL(G) (G).begin(),(G).end() typedef long long ll; typedef double D; typedef pair<int,int> pii; typedef vector<int> vi; const int inft = 1000000009; const int MAXN = 1000006,PT=41; const ll bound=1LL<<PT; int A[MAXN],K[PT+1]; pii DP[PT+1][MAXN]; char str[MAXN]; int n,m,k,u; int S[MAXN]; pii W[MAXN]; ll step[MAXN]; bool never[MAXN]; inline int ADD(int a,int b){ if(a+b>=m)return a+b-m; return a+b; } void solve() { scanf("%d",&n); fru(i,n)scanf("%d",A+i); scanf("%d",&m); k=n%m; scanf("%s",str); fru(i,m){ u=str[i]=='W'?1:-1; DP[0][i]=pii(u,u); } K[0]=k; fru(i,PT+1)if(i)K[i]=ADD(K[i-1],K[i-1]); fru(i,PT+1)if(i){ fru(j,m){ u=ADD(j,K[i-1]); DP[i][j].x=DP[i-1][j].x+ DP[i-1][u].x; DP[i][j].y=min(DP[i-1][j].y,DP[i-1][j].x+ DP[i-1][u].y); if(DP[i][j].x>inft)DP[i][j].x=inft; if(DP[i][j].x<-inft)DP[i][j].x=-inft; if(DP[i][j].y<-inft)DP[i][j].y=-inft; } } ll ans=bound*n; fru(i,n){ step[i]=0;W[i]=pii(0,0); S[i]=i%m; never[i]=0; } for(int j=PT;j>=0;j--)fru(i,n)if(!never[i]){ pii temp; temp.x=W[i].x+DP[j][S[i]].x; temp.y=min(W[i].y,W[i].x+ DP[j][S[i]].y); // printf("s= %d temp %d, %d, w %d %d\n",s,temp.x,temp.y,w.x,w.y); if(temp.y+A[i]<=0){ continue; } else { if(j==PT){never[i]=true;} W[i]=temp; if(W[i].x>inft)W[i].x=inft; if(W[i].x<-inft)W[i].x=-inft; S[i]=ADD(S[i],K[j]); step[i]+=1LL<<(j); } } fru(i,n)if(!never[i])ans=min(ans,1LL+i+(step[i])*n); if(ans==bound*n)puts("-1"); else printf("%lld\n",ans); } int main() { // freopen("input.in", "r", stdin); // freopen("output.out", "w", stdout); int t=1; // scanf("%d",&t); fru(i,t) solve(); return 0; } |