#include <cstdio> #include <vector> using namespace std; vector <int> cycle[1000005]; vector < int > przyn[1000005]; vector < long long > pref; vector < pair < long long, int > > best_forward, best_pref; int where[1000005], cycle_pos[1000005]; long long koszt[1000005], bandyta[1000005]; int n,m, next_nr = 1; int forward(int a){ return (a-1+n) % m + 1; } int back(int a){ return (a-1-n+m) % m + 1; } inline long long min(long long a, long long b){ if (a < b) return a; return b; } inline long long max(long long a, long long b){ if (a > b) return a; return b; } inline long long ceil(long long a, long long b){ if (a % b == 0){ return a/b; } else { return a/b + 1; } } void process(int st){ int start = st; cycle[next_nr].push_back(0); int cycle_count = 1; do { cycle[next_nr].push_back(start); where[start] = next_nr; cycle_pos[start] = cycle_count; start = forward(start); cycle_count++; } while (start != st); } pair <long long,int> drzewo[4000005]; void add_tree(int pos, int s, int cs, int ce, pair <long long,int> v){ if ((s == cs) && (s == ce)){ drzewo[pos] = v; } else if (s <= (cs+ce)/2){ add_tree(pos*2, s, cs, (cs+ce)/2, v); if (drzewo[pos*2].first <= drzewo[pos*2+1].first) drzewo[pos] = drzewo[pos*2]; else drzewo[pos] = drzewo[pos*2+1]; } else { add_tree(pos*2+1, s, (cs+ce)/2+1, ce, v); if (drzewo[pos*2].first <= drzewo[pos*2+1].first) drzewo[pos] = drzewo[pos*2]; else drzewo[pos] = drzewo[pos*2+1]; } } pair <long long, int> get_tree(int pos, int s, int e, int cs, int ce, long long v){ //printf(":: %d %d %d %d %d\n", pos, s, e, cs, ce); if ((s == cs) && (e == ce)){ if (drzewo[pos].first + v > 0){ return make_pair(0,-1); } else if (s == e){ return drzewo[pos]; } else { pair <long long, int> t = get_tree(pos*2, s, (cs+ce)/2, cs, (cs+ce)/2, v); if (t.second == -1){ return get_tree(pos*2+1, (cs+ce)/2+1, e, (cs+ce)/2+1, ce, v); } else { return t; } } } else if ((s <= (cs+ce)/2) && (e <= (cs+ce)/2)){ return get_tree(pos*2, s, e, cs, (cs+ce)/2, v); } else if ((s > (cs+ce)/2) && (e > (cs+ce)/2)){ return get_tree(pos*2+1, s, e, (cs+ce)/2+1, ce, v); } else { pair <long long, int> t = get_tree(pos*2, s, (cs+ce)/2, cs, (cs+ce)/2, v); if (t.second == -1){ return get_tree(pos*2+1, (cs+ce)/2+1, e, (cs+ce)/2+1, ce, v); } else { return t; } } } int main(){ scanf("%d", &n); for (int i = 1; i <= n; i++){ scanf("%lld", &koszt[i]); } scanf("%d", &m); for (int i = 1; i <= m; i++){ char t; scanf(" %c", &t); if (t == 'W'){ bandyta[i] = 1; } else { bandyta[i] = -1; } //printf("%lld ", bandyta[i]); } //printf("\n"); for (int i = 1; i <= m; i++){ if (where[i] == 0){ process(i); next_nr++; } } for (int i = 1; i <= n; i++){ przyn[ where[ (i-1) % m + 1 ] ].push_back(i); } long long best = 1000000000000000005LL; //printf("a %d\n", next_nr); for (int i = 1; i < next_nr; i++){ /*for (int j = 0; j < przyn[i].size(); j++){ printf("%d ", przyn[i][j]); } printf("\n");*/ pref.push_back(0); best_pref.push_back(make_pair(0,0)); for (int j = 1; j < cycle[i].size(); j++){ // printf("%d ", cycle[i][j]); pref.push_back(bandyta[ cycle[i][j] ] + pref[pref.size()-1]); } //printf("\n"); for (int j = 1; j < cycle[i].size(); j++){ if (pref[j] < best_pref[j-1].first){ best_pref.push_back(make_pair(pref[j],j)); } else { best_pref.push_back(best_pref[best_pref.size()-1]); } } /*for (int j = 0; j < cycle[i].size(); j++){ printf("%lld ", pref[j]); } printf("\n"); for (int j = 0; j < cycle[i].size(); j++){ printf("%lld ", best_pref[j]); } printf("\n");*/ best_forward.resize( cycle[i].size() ); best_forward[ cycle[i].size()-1 ] = make_pair(pref[ cycle[i].size()-1 ], cycle[i].size()-1); for (int j = cycle[i].size()-2; j >= 1; j--){ if (pref[j] < best_forward[j+1].first){ best_forward[j] = make_pair(pref[j], j); } else { best_forward[j] = best_forward[j+1]; } } /*for (int j = 1; j < cycle[i].size(); j++){ printf("%lld ", best_forward[j]); } printf("\n");*/ for (int j = 1; j < cycle[i].size(); j++){ add_tree(1, j, 1, cycle[i].size()-1, make_pair(pref[j],j)); } //printf("a\n"); for (int k = 0; k < przyn[i].size(); k++){ long long round = 1000000000000000005LL; int j = cycle_pos[ (przyn[i][k] - 1) % m + 1 ]; //printf("%d %d\n", przyn[i][k], j); //printf("wuwuzela %d %d\n", (przyn[i][k] - 1) % m + 1, j); if (best_forward[j].first - pref[j-1] + koszt[ przyn[i][k] ] <= 0){ round = min(round, 0); } else if (pref[ cycle[i].size()-1 ] < 0){ round = min(round, ceil(((best_forward[j].first - pref[j-1]) + koszt[ przyn[i][k] ]), (-pref[ cycle[i].size()-1 ]))); } //printf("zeta\n"); if (round < 1000000000000000005LL){ long long kos = koszt[ przyn[i][k] ] + round * pref[ cycle[i].size()-1 ] - pref[j-1]; //printf("ww2 %lld %lld\n", kos, round); pair < long long, int > t = get_tree(1, j, cycle[i].size()-1, 1, cycle[i].size()-1, kos); //printf("cc2 %lld %d\n", t.first, t.second); //printf("dupa2 %d %lld %d %d", przyn[i][k], round * (cycle[i].size()-1), t.second, j); if (t.second != -1){ best = min(best, przyn[i][k] + n * (round * (cycle[i].size()-1) + t.second - j)); } } //printf("buta\n"); } //printf("b\n"); for (int k = 0; k < przyn[i].size(); k++){ long long round = 1000000000000000005LL; int j = cycle_pos[ (przyn[i][k] - 1) % m + 1 ]; if (pref[ cycle[i].size()-1 ] + best_pref[j-1].first - pref[j-1] + koszt[ przyn[i][k] ] <= 0){ //printf("c\n"); round = min(round, 0); } else if (pref[ cycle[i].size()-1 ] < 0){ //printf("d %lf\n", ceil((pref[ cycle[i].size()-1 ] + best_pref[j-1].first - pref[j-1] + koszt[ cycle[i][j] ]) / (double)(-pref[ cycle[i].size()-1 ]))); round = min(round, ceil((pref[ cycle[i].size()-1 ] + best_pref[j-1].first - pref[j-1] + koszt[ przyn[i][k] ]), (-pref[ cycle[i].size()-1 ]))); } if ((round < 1000000000000000005LL) && (j > 1)){ long long kos = koszt[ przyn[i][k] ] + round * pref[ cycle[i].size()-1 ] + pref[ cycle[i].size()-1 ] - pref[j-1]; pair < long long, int > t = get_tree(1, 1, j-1, 1, cycle[i].size()-1, kos); //printf("wwwwwwwwwwwwwwww0 %lld %lld %lld %d %d\n", kos, round, t.first, t.second, j); if (t.second != -1){ best = min(best, przyn[i][k] + n * (round * (cycle[i].size()-1) + t.second + (cycle[i].size()-1) - j)); //printf("ww1 %lld %lld\n", kos, round); //printf("cc1 %lld %d\n", t.first, t.second); //printf("dupa1 %d %lld %d %d", przyn[i][k], round * (cycle[i].size()-1), t.second, j); } } } best_pref.clear(); best_forward.clear(); pref.clear(); } if (best == 1000000000000000005LL){ printf("-1\n"); } else { printf("%lld\n", best); } }
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 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 | #include <cstdio> #include <vector> using namespace std; vector <int> cycle[1000005]; vector < int > przyn[1000005]; vector < long long > pref; vector < pair < long long, int > > best_forward, best_pref; int where[1000005], cycle_pos[1000005]; long long koszt[1000005], bandyta[1000005]; int n,m, next_nr = 1; int forward(int a){ return (a-1+n) % m + 1; } int back(int a){ return (a-1-n+m) % m + 1; } inline long long min(long long a, long long b){ if (a < b) return a; return b; } inline long long max(long long a, long long b){ if (a > b) return a; return b; } inline long long ceil(long long a, long long b){ if (a % b == 0){ return a/b; } else { return a/b + 1; } } void process(int st){ int start = st; cycle[next_nr].push_back(0); int cycle_count = 1; do { cycle[next_nr].push_back(start); where[start] = next_nr; cycle_pos[start] = cycle_count; start = forward(start); cycle_count++; } while (start != st); } pair <long long,int> drzewo[4000005]; void add_tree(int pos, int s, int cs, int ce, pair <long long,int> v){ if ((s == cs) && (s == ce)){ drzewo[pos] = v; } else if (s <= (cs+ce)/2){ add_tree(pos*2, s, cs, (cs+ce)/2, v); if (drzewo[pos*2].first <= drzewo[pos*2+1].first) drzewo[pos] = drzewo[pos*2]; else drzewo[pos] = drzewo[pos*2+1]; } else { add_tree(pos*2+1, s, (cs+ce)/2+1, ce, v); if (drzewo[pos*2].first <= drzewo[pos*2+1].first) drzewo[pos] = drzewo[pos*2]; else drzewo[pos] = drzewo[pos*2+1]; } } pair <long long, int> get_tree(int pos, int s, int e, int cs, int ce, long long v){ //printf(":: %d %d %d %d %d\n", pos, s, e, cs, ce); if ((s == cs) && (e == ce)){ if (drzewo[pos].first + v > 0){ return make_pair(0,-1); } else if (s == e){ return drzewo[pos]; } else { pair <long long, int> t = get_tree(pos*2, s, (cs+ce)/2, cs, (cs+ce)/2, v); if (t.second == -1){ return get_tree(pos*2+1, (cs+ce)/2+1, e, (cs+ce)/2+1, ce, v); } else { return t; } } } else if ((s <= (cs+ce)/2) && (e <= (cs+ce)/2)){ return get_tree(pos*2, s, e, cs, (cs+ce)/2, v); } else if ((s > (cs+ce)/2) && (e > (cs+ce)/2)){ return get_tree(pos*2+1, s, e, (cs+ce)/2+1, ce, v); } else { pair <long long, int> t = get_tree(pos*2, s, (cs+ce)/2, cs, (cs+ce)/2, v); if (t.second == -1){ return get_tree(pos*2+1, (cs+ce)/2+1, e, (cs+ce)/2+1, ce, v); } else { return t; } } } int main(){ scanf("%d", &n); for (int i = 1; i <= n; i++){ scanf("%lld", &koszt[i]); } scanf("%d", &m); for (int i = 1; i <= m; i++){ char t; scanf(" %c", &t); if (t == 'W'){ bandyta[i] = 1; } else { bandyta[i] = -1; } //printf("%lld ", bandyta[i]); } //printf("\n"); for (int i = 1; i <= m; i++){ if (where[i] == 0){ process(i); next_nr++; } } for (int i = 1; i <= n; i++){ przyn[ where[ (i-1) % m + 1 ] ].push_back(i); } long long best = 1000000000000000005LL; //printf("a %d\n", next_nr); for (int i = 1; i < next_nr; i++){ /*for (int j = 0; j < przyn[i].size(); j++){ printf("%d ", przyn[i][j]); } printf("\n");*/ pref.push_back(0); best_pref.push_back(make_pair(0,0)); for (int j = 1; j < cycle[i].size(); j++){ // printf("%d ", cycle[i][j]); pref.push_back(bandyta[ cycle[i][j] ] + pref[pref.size()-1]); } //printf("\n"); for (int j = 1; j < cycle[i].size(); j++){ if (pref[j] < best_pref[j-1].first){ best_pref.push_back(make_pair(pref[j],j)); } else { best_pref.push_back(best_pref[best_pref.size()-1]); } } /*for (int j = 0; j < cycle[i].size(); j++){ printf("%lld ", pref[j]); } printf("\n"); for (int j = 0; j < cycle[i].size(); j++){ printf("%lld ", best_pref[j]); } printf("\n");*/ best_forward.resize( cycle[i].size() ); best_forward[ cycle[i].size()-1 ] = make_pair(pref[ cycle[i].size()-1 ], cycle[i].size()-1); for (int j = cycle[i].size()-2; j >= 1; j--){ if (pref[j] < best_forward[j+1].first){ best_forward[j] = make_pair(pref[j], j); } else { best_forward[j] = best_forward[j+1]; } } /*for (int j = 1; j < cycle[i].size(); j++){ printf("%lld ", best_forward[j]); } printf("\n");*/ for (int j = 1; j < cycle[i].size(); j++){ add_tree(1, j, 1, cycle[i].size()-1, make_pair(pref[j],j)); } //printf("a\n"); for (int k = 0; k < przyn[i].size(); k++){ long long round = 1000000000000000005LL; int j = cycle_pos[ (przyn[i][k] - 1) % m + 1 ]; //printf("%d %d\n", przyn[i][k], j); //printf("wuwuzela %d %d\n", (przyn[i][k] - 1) % m + 1, j); if (best_forward[j].first - pref[j-1] + koszt[ przyn[i][k] ] <= 0){ round = min(round, 0); } else if (pref[ cycle[i].size()-1 ] < 0){ round = min(round, ceil(((best_forward[j].first - pref[j-1]) + koszt[ przyn[i][k] ]), (-pref[ cycle[i].size()-1 ]))); } //printf("zeta\n"); if (round < 1000000000000000005LL){ long long kos = koszt[ przyn[i][k] ] + round * pref[ cycle[i].size()-1 ] - pref[j-1]; //printf("ww2 %lld %lld\n", kos, round); pair < long long, int > t = get_tree(1, j, cycle[i].size()-1, 1, cycle[i].size()-1, kos); //printf("cc2 %lld %d\n", t.first, t.second); //printf("dupa2 %d %lld %d %d", przyn[i][k], round * (cycle[i].size()-1), t.second, j); if (t.second != -1){ best = min(best, przyn[i][k] + n * (round * (cycle[i].size()-1) + t.second - j)); } } //printf("buta\n"); } //printf("b\n"); for (int k = 0; k < przyn[i].size(); k++){ long long round = 1000000000000000005LL; int j = cycle_pos[ (przyn[i][k] - 1) % m + 1 ]; if (pref[ cycle[i].size()-1 ] + best_pref[j-1].first - pref[j-1] + koszt[ przyn[i][k] ] <= 0){ //printf("c\n"); round = min(round, 0); } else if (pref[ cycle[i].size()-1 ] < 0){ //printf("d %lf\n", ceil((pref[ cycle[i].size()-1 ] + best_pref[j-1].first - pref[j-1] + koszt[ cycle[i][j] ]) / (double)(-pref[ cycle[i].size()-1 ]))); round = min(round, ceil((pref[ cycle[i].size()-1 ] + best_pref[j-1].first - pref[j-1] + koszt[ przyn[i][k] ]), (-pref[ cycle[i].size()-1 ]))); } if ((round < 1000000000000000005LL) && (j > 1)){ long long kos = koszt[ przyn[i][k] ] + round * pref[ cycle[i].size()-1 ] + pref[ cycle[i].size()-1 ] - pref[j-1]; pair < long long, int > t = get_tree(1, 1, j-1, 1, cycle[i].size()-1, kos); //printf("wwwwwwwwwwwwwwww0 %lld %lld %lld %d %d\n", kos, round, t.first, t.second, j); if (t.second != -1){ best = min(best, przyn[i][k] + n * (round * (cycle[i].size()-1) + t.second + (cycle[i].size()-1) - j)); //printf("ww1 %lld %lld\n", kos, round); //printf("cc1 %lld %d\n", t.first, t.second); //printf("dupa1 %d %lld %d %d", przyn[i][k], round * (cycle[i].size()-1), t.second, j); } } } best_pref.clear(); best_forward.clear(); pref.clear(); } if (best == 1000000000000000005LL){ printf("-1\n"); } else { printf("%lld\n", best); } } |