#include <bits/stdc++.h> using namespace std; const int MAXN = 1000000; int n, m; int money[MAXN + 5], start_money[MAXN + 5]; int val[MAXN + 5]; int cnt[MAXN + 5]; char word[MAXN + 5]; int main() { scanf("%d", &n); for(int i = 0; i < n; ++i) { scanf("%d", &money[i]); start_money[i] = money[i]; } scanf("%d %s", &m, word); for(int i = 0; i < m; ++i) { if(word[i] == 'W') val[i] = 1; else val[i] = -1; } int pos = 0, moves = 0; while(1) { for(int i = 0; i < n; ++i) { money[i] += val[pos]; pos = (pos + 1) % m; cnt[i]++; moves++; if(money[i] == 0) { printf("%d\n", moves); return 0; } if(cnt[i] > max(m, n) && money[i] >= start_money[i]) { printf("-1\n"); return 0; } } } 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 | #include <bits/stdc++.h> using namespace std; const int MAXN = 1000000; int n, m; int money[MAXN + 5], start_money[MAXN + 5]; int val[MAXN + 5]; int cnt[MAXN + 5]; char word[MAXN + 5]; int main() { scanf("%d", &n); for(int i = 0; i < n; ++i) { scanf("%d", &money[i]); start_money[i] = money[i]; } scanf("%d %s", &m, word); for(int i = 0; i < m; ++i) { if(word[i] == 'W') val[i] = 1; else val[i] = -1; } int pos = 0, moves = 0; while(1) { for(int i = 0; i < n; ++i) { money[i] += val[pos]; pos = (pos + 1) % m; cnt[i]++; moves++; if(money[i] == 0) { printf("%d\n", moves); return 0; } if(cnt[i] > max(m, n) && money[i] >= start_money[i]) { printf("-1\n"); return 0; } } } return 0; } |