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
#include <bits/stdc++.h>
#include <string>
using namespace std;

template<typename T>
inline ostream& operator<< (ostream& out, const vector<T>& data) {
    out << data[0];
    for( auto it = data.begin()+1; it != data.end(); it++ )
        out << ' ' << *it;
    return out;
}

template<typename T>
inline ostream& operator<< (ostream& out, const queue<T>& data) {
    switch( data.size() ) {
        case 0: return out << "[]";
        case 1: return out << "[ " << data.front() << " ]";
        default: return out << "[ " << data.front() << " ... ]";
    }
}

template<typename T>
inline istream& operator>> (istream& in, vector<T>& data) {
    for( auto &v : data )
        in >> v;
    return in;
}

template<typename A, typename B>
inline ostream& operator<< (ostream& out, const pair<A, B>& data) {
    return out << data.first << ' ' << data.second;
}

int gcd(int a, int b) { return b==0 ? a : gcd(b, a%b); }

long long test( vector<int>& players, vector<char>& moves ) {
    //cerr << players << ", " << moves << endl;
    int step = players.size() % moves.size();
    int high = 0;
    int sum = 0;
    int low = 0;
    
    auto update_score = [](int &score, char move) {
          score += (move == 'W' ? 1 : -1);
    };
    
    for( int i = 0, m = 0; i < moves.size(); i++ ) {
        update_score( sum, moves[m] );
        high = max( high, sum );
        low = min( low, sum );
        m = (m+step) % moves.size();
    }
    
    low = min( low, sum+low );
    high = max( high, sum+high );
    
    queue<int> levels[high-low+1];
    
    int min_score = 0;
    auto level_push = [&min_score, &levels, low](int score, int pos) {
        levels[score-low].push(pos);
        if( min_score > score )
            min_score = score;
    };
    auto level_pop = [&min_score, &levels, low](int score) {
        levels[score-low].pop();
        while( levels[min_score-low].empty() )
            min_score++;
    };
    auto level_get = [&levels, low](int score)->int {
        return levels[score-low].front();
    };
    
    
    int score_end = 0;
    level_push( score_end, -1 );
    for( int i = 0, m = 0; i < moves.size(); i++ ) {
        update_score( score_end, moves[m] );
        level_push( score_end, i );
        m = (m+step) % moves.size();
    }
    
    long long answer = numeric_limits<long long>::max();
    int score_begin = 0;
    /*for( int i = low; i <= high; i++ )
        cerr << i << ' ';
    cerr << endl;*/
    for( int i = 0, m = 0; i < moves.size(); i++ ) {
        /*for( int i = low; i <= high; i++ )
            cerr << levels[i-low] << ' ';
        cerr << endl;*/
    
        for( int p = m; p < players.size(); p += moves.size() ) {
            //cerr << "Player " << p << " requires " << players[p] << endl;
            int from_loops = max( 0, players[p] - (score_begin-min_score) );
            //cerr << " from_loops: " << from_loops << endl;
            
            if( from_loops > 0 and sum >= 0 )
                continue;
                
            int loops = from_loops == 0 ? 0 : (from_loops-sum-1)/-sum;
            int score_diff = players[p] + loops*sum;
            //cerr << " searching for: " << score_diff << endl;
            int x = level_get(score_begin-score_diff) - i;
            
            //cerr << " answer-data: " << p << ' ' << loops << ' ' << x << endl;
            long long loses = p + 1 + 1LL*loops*players.size()*moves.size() + 1LL*players.size()*x;
            //cerr << " answer-data: " << loses << endl;
            answer = min( answer, loses );
        }
        
        //cerr << scores << " | " << min_scores << endl;
        
        //cerr << zero << ' ' << max_score << endl;
        //cerr << levels << endl;
        //assert( levels[-zero+max_score].front() == i );
        
        //assert( level_get(score_begin) == i );
        level_pop( score_begin );
        update_score( score_begin, moves[m] );
        update_score( score_end, moves[m] );
        level_push( score_end, i+moves.size() );
        m = (m+step) % moves.size();
    }
    

    return answer;
}

void test() {
    int n;
    cin >> n;
    vector<int> players(n);
    cin >> players;
    
    int m;
    cin >> m;
    vector<char> moves(m);
    cin >> moves;
    
    int d = gcd(n, m);
    
    vector<int> players_skip(n/d);
    vector<char> moves_skip(m/d);
    
    long long answer = numeric_limits<long long>::max();
    for( int i = 0; i < d; i++ ) {
        for( int j = 0; j < n/d; j++ )
            players_skip[j] = players[d*j+i];
            
        for( int j = 0; j < m/d; j++ )
            moves_skip[j] = moves[d*j+i];
            
        long long result = test(players_skip, moves_skip);
        if( result != numeric_limits<long long>::max() )
            answer = min( answer, result*d+i-d+1 );
    }
   
    if( answer == numeric_limits<long long>::max() )
        cout << "-1\n";
    else
        cout << answer << '\n';
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int T = 1;
    //cin >> T;
    while( T --> 0 )
        test();
    return 0;
}