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
#include <iostream>
#include<vector>
#include <algorithm>
#include <math.h>



using namespace std;

struct person{
    vector <unsigned int> wins;
    int money;
    int lose_per_cycle;
} *ludzie;

char *cykl;
int n,m;
int cycle;



int win_cycle(int punkt){
    int first_person,cur_per,turn,counter,last_person;
    int i;
    if(n>=m)
        first_person=punkt;
    if(n<m){
        if((punkt+1)%n==0)
        first_person=n-1;
        else
        first_person=((punkt+1)%n)-1;
    }
    if(n>=m)
        ludzie[first_person].wins.push_back(1);
    if(n<m)
        ludzie[first_person].wins.push_back(ceil((punkt+1)/n));
    i=1;
    turn=1;
    counter=1;
    last_person=first_person;
    do{
        if(n==m){
            cur_per=first_person;
            turn=2;
        }
        if(n>m){
            if((punkt+1)+m*i>n*counter){
                cur_per=(punkt+1)+m*i-n*counter-1;
                counter++;
            }
            else
                cur_per=(punkt+1)+m-1;
            if(cur_per>last_person)
                turn=1;
           else
                turn++;
        }
        if(n<m){
            if((punkt+1)+(m*i)%n==0)
                cur_per=n-1;
            else
                cur_per=((punkt+1)+(m*i)%n)-1;

            turn=ceil(((punkt+1)+(m*i))/(double)n);
        }
        i++;
        ludzie[cur_per].wins.push_back(turn);
    //    cout<<"cur:cur_per "<<cur_per<<"cur point:"<<punkt<<"cur turn:"<<turn<<"\n";
        last_person=cur_per;
    }while(!(first_person==cur_per));
    cycle=turn;
}

int win_search(){
    int i;
    for(i=0;i<m;i++){
        if(cykl[i]=='W') win_cycle(i);
    }

}

int when_lose(int kto){
    int i,start,koniec;
    start=ludzie[kto].money;
    koniec=ludzie[kto].money;
    if(ludzie[kto].wins.size()==0) return ludzie[kto].money;
    for(i=0;i<ludzie[kto].wins.size();i++){
        if(i>0)
        koniec = koniec-(ludzie[kto].wins[i]-2-ludzie[kto].wins[i-1]);
    else koniec= koniec-(ludzie[kto].wins[i]-2);
       // if(ludzie[kto].money<=1) return ludzie[kto].wins[i]+abs(ludzie[kto].money);
    }
 //    cout<<kto<<" ";
 //   cout<<koniec<<" ";
 //   cout<<ludzie[kto].wins[i-1]<<" ";
    koniec = koniec-(cycle-1-ludzie[kto].wins[i-1]);
    if(start>koniec){
     //       cout<<koniec<<" ";
     //       cout<<(start/(start-koniec))<<"\n";
            ludzie[kto].lose_per_cycle=start-koniec;
        return (start/(start-koniec));
    }
  //  cout<<" ";
    return -1;
}

int finally(int kto,int kiedy){
    int i;
    ludzie[kto].money=ludzie[kto].money-ludzie[kto].lose_per_cycle*kiedy;
    for(i=0;i<ludzie[kto].wins.size();i++){
    if(i>0)
        ludzie[kto].money = ludzie[kto].money-(ludzie[kto].wins[i]-2-ludzie[kto].wins[i-1]);
    else ludzie[kto].money= ludzie[kto].money-(ludzie[kto].wins[i]-2);
        if(ludzie[kto].money<=1) return ludzie[kto].wins[i]-abs(ludzie[kto].money-2)+(kiedy)*(kto+1)*(cycle-1);
    }
    return (kto+1)*(cycle-1)*(kiedy+1)+ludzie[kto].money;
}

int main()
{
    int money,lowest,current,lowesti;
    int i;
    cin>>n;
    lowest=-2;
    ludzie = new person[n+1];
    for(i=0;i<n;i++){
        cin>>ludzie[i].money;
    }
    cin>>m;
    cykl = new char[m];
    cin>>cykl;
    win_search();
    for(i=0;i<n;i++){
        current=when_lose(i);
        if(current>-1)
            if(lowest==-2){
                lowest=current;
                lowesti=i;
            }
           else
            if(lowest>current){
                lowest=current;
                lowesti=i;
            }
    }
    if(lowest==-2)
        cout<<"-1";
    else
        cout<<finally(lowesti,lowest);
}