#include <set> #include <map> #include <cmath> #include <queue> #include <stack> #include <string> #include <cstdio> #include <vector> #include <utility> #include <cstdlib> #include <iostream> #include <algorithm> #define ST first #define ND second #define MP make_pair #define PB push_back #define QI queue <int> #define VI vector <int> #define PII pair <int,int> #define SZ(x) (int)(x).size() #define ALL(x) x.begin(),x.end() #define RALL(x) x.rbegin(),x.rend() #define SYNC ios_base::sync_with_stdio(0) #define MAX(a,b) ((a) > (b)) ? a : b using namespace std; typedef long long LL; typedef long double LD; typedef unsigned int UI; typedef unsigned long long ULL; const LL INF = 1e18 + 7; const int ZERO = 1000005; int a, b, c, d, e, f, n, m, k, nxt, sum, change, srodek = ZERO, bilans, lewo, prawo, last, first; LL res = INF; char Maszyna[1000005]; int Ciag[1000005]; int Money[1000005]; int Index[2000005]; int War[1000005]; bool Started[1000005]; bool Inside[1000005]; vector <int> Cycle; vector <int> Same; vector <int> Wyst[2000010]; queue <int> MemoryQ; void StartSolve(int); void Solve(int); void CalcWyst(); void MoveIt(int); void ClearShiet(); int main() { scanf("%d", &n); for(int i = 1; i <= n; i++) { scanf("%d", &Money[i]); } scanf("%d", &m); scanf("%s", Maszyna); for(int i = 1; i <= m; i++) { Ciag[i] = (Maszyna[i - 1] == 'W') ? 1 : -1; bilans += Ciag[i]; } nxt = n % m; for(int i = 1; i <= n; i++) { if(!Started[i]) { StartSolve(i); CalcWyst(); for(int j = 0; j < SZ(Cycle); j++) { Solve(Cycle[j]); MoveIt(Cycle[j]); } ClearShiet(); } } printf("%lld\n", (res == INF) ? -1 : res); } void StartSolve(int x) { int act = x; while(!Inside[act]) { Inside[act] = true; Cycle.PB(act); act += nxt; if(act > m) { act -= m; } } } void CalcWyst() { sum = first = lewo = prawo = last = 0; srodek = ZERO; for(int i = 0; i < SZ(Cycle); i++) { sum += Ciag[Cycle[i]]; War[i] = sum; Wyst[srodek + sum].PB(last++); MemoryQ.push(srodek + sum); if(lewo == 0 && prawo == 0) { lewo = prawo = sum; } else { lewo = min(lewo, sum); prawo = max(prawo, sum); } } bilans = sum; } void Solve(int x) { if(x <= n) { while(x <= n) { Started[x] = true; Same.PB(x); x += m; } while(!Same.empty()) { int v = Same.back(); Same.pop_back(); LL wynik = 0; if(-lewo >= Money[v]) { int duba = Wyst[srodek - Money[v]][Index[srodek - Money[v]]] - first + 1; wynik = (LL)n * (LL)(duba - 1) + (LL)v; res = min(res, wynik); } if(wynik == 0 && bilans < 0) { LL ile = (Money[v] + lewo - bilans - 1) / -bilans; LL zostalo = Money[v] + ile * bilans; int duba = Wyst[srodek - zostalo][Index[srodek - zostalo]] - first + 1; wynik = (LL)ile * (LL)SZ(Cycle) * (LL)n + (LL)n * (LL)(duba - 1) + (LL)v; res = min(res, wynik); } } } } void MoveIt(int x) { Index[srodek + Ciag[x]]++; if(Index[srodek + Ciag[x]] == SZ(Wyst[srodek + Ciag[x]])) { if(Ciag[x] == -1) { if(lewo < 0 && prawo < 0) { prawo--; } else { lewo++; } } else { if(lewo > 0 && prawo > 0) { lewo++; } else { prawo--; } } } srodek += Ciag[x]; lewo += -Ciag[x]; prawo += -Ciag[x]; lewo = min(lewo, 1); prawo = max(prawo, -1); Wyst[srodek + bilans].PB(last++); if(Index[srodek + bilans] == SZ(Wyst[srodek + bilans]) || SZ(Wyst[srodek + bilans]) == 1) { (bilans > 0) ? prawo++ : lewo--; } MemoryQ.push(srodek + bilans); first++; } void ClearShiet() { Cycle.clear(); while(!MemoryQ.empty()) { Wyst[MemoryQ.front()].clear(); Index[MemoryQ.front()] = 0; MemoryQ.pop(); } }
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 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 | #include <set> #include <map> #include <cmath> #include <queue> #include <stack> #include <string> #include <cstdio> #include <vector> #include <utility> #include <cstdlib> #include <iostream> #include <algorithm> #define ST first #define ND second #define MP make_pair #define PB push_back #define QI queue <int> #define VI vector <int> #define PII pair <int,int> #define SZ(x) (int)(x).size() #define ALL(x) x.begin(),x.end() #define RALL(x) x.rbegin(),x.rend() #define SYNC ios_base::sync_with_stdio(0) #define MAX(a,b) ((a) > (b)) ? a : b using namespace std; typedef long long LL; typedef long double LD; typedef unsigned int UI; typedef unsigned long long ULL; const LL INF = 1e18 + 7; const int ZERO = 1000005; int a, b, c, d, e, f, n, m, k, nxt, sum, change, srodek = ZERO, bilans, lewo, prawo, last, first; LL res = INF; char Maszyna[1000005]; int Ciag[1000005]; int Money[1000005]; int Index[2000005]; int War[1000005]; bool Started[1000005]; bool Inside[1000005]; vector <int> Cycle; vector <int> Same; vector <int> Wyst[2000010]; queue <int> MemoryQ; void StartSolve(int); void Solve(int); void CalcWyst(); void MoveIt(int); void ClearShiet(); int main() { scanf("%d", &n); for(int i = 1; i <= n; i++) { scanf("%d", &Money[i]); } scanf("%d", &m); scanf("%s", Maszyna); for(int i = 1; i <= m; i++) { Ciag[i] = (Maszyna[i - 1] == 'W') ? 1 : -1; bilans += Ciag[i]; } nxt = n % m; for(int i = 1; i <= n; i++) { if(!Started[i]) { StartSolve(i); CalcWyst(); for(int j = 0; j < SZ(Cycle); j++) { Solve(Cycle[j]); MoveIt(Cycle[j]); } ClearShiet(); } } printf("%lld\n", (res == INF) ? -1 : res); } void StartSolve(int x) { int act = x; while(!Inside[act]) { Inside[act] = true; Cycle.PB(act); act += nxt; if(act > m) { act -= m; } } } void CalcWyst() { sum = first = lewo = prawo = last = 0; srodek = ZERO; for(int i = 0; i < SZ(Cycle); i++) { sum += Ciag[Cycle[i]]; War[i] = sum; Wyst[srodek + sum].PB(last++); MemoryQ.push(srodek + sum); if(lewo == 0 && prawo == 0) { lewo = prawo = sum; } else { lewo = min(lewo, sum); prawo = max(prawo, sum); } } bilans = sum; } void Solve(int x) { if(x <= n) { while(x <= n) { Started[x] = true; Same.PB(x); x += m; } while(!Same.empty()) { int v = Same.back(); Same.pop_back(); LL wynik = 0; if(-lewo >= Money[v]) { int duba = Wyst[srodek - Money[v]][Index[srodek - Money[v]]] - first + 1; wynik = (LL)n * (LL)(duba - 1) + (LL)v; res = min(res, wynik); } if(wynik == 0 && bilans < 0) { LL ile = (Money[v] + lewo - bilans - 1) / -bilans; LL zostalo = Money[v] + ile * bilans; int duba = Wyst[srodek - zostalo][Index[srodek - zostalo]] - first + 1; wynik = (LL)ile * (LL)SZ(Cycle) * (LL)n + (LL)n * (LL)(duba - 1) + (LL)v; res = min(res, wynik); } } } } void MoveIt(int x) { Index[srodek + Ciag[x]]++; if(Index[srodek + Ciag[x]] == SZ(Wyst[srodek + Ciag[x]])) { if(Ciag[x] == -1) { if(lewo < 0 && prawo < 0) { prawo--; } else { lewo++; } } else { if(lewo > 0 && prawo > 0) { lewo++; } else { prawo--; } } } srodek += Ciag[x]; lewo += -Ciag[x]; prawo += -Ciag[x]; lewo = min(lewo, 1); prawo = max(prawo, -1); Wyst[srodek + bilans].PB(last++); if(Index[srodek + bilans] == SZ(Wyst[srodek + bilans]) || SZ(Wyst[srodek + bilans]) == 1) { (bilans > 0) ? prawo++ : lewo--; } MemoryQ.push(srodek + bilans); first++; } void ClearShiet() { Cycle.clear(); while(!MemoryQ.empty()) { Wyst[MemoryQ.front()].clear(); Index[MemoryQ.front()] = 0; MemoryQ.pop(); } } |