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
#include <iostream>
#include <cmath>
#include <vector>

using namespace std;

int players[1000001];
int game[1000001];
int cycle[1000001];
int mincycle[1000001];
vector<int> val[1000001]; 
int lcm=200000001;

int main()
{
	int n, m;
	cin >> n;
	for(int i=0; i<n; i++)
		cin >> players[i];
	cin >> m;
	for(int i=0; i<m; i++)
	{
		char c;
		cin >> c;
		if(c=='W')
			game[i] = 1;
		else
			game[i] = -1;
	}
	for(int i=0; i<n; i++)
	{
		cycle[i]=0;
		mincycle[i]=lcm+1;
	}
	for(int i=0; i<n; i++)
	{
		int a = i;
		a %= m;
		cycle[i] += game[a];
		val[i].push_back(game[a]);
		if(cycle[i] < mincycle[i])
				mincycle[i] = cycle[i];

		int iter=0;	
		while(true)
		{
			a += n;
			a %= m;
			if(a == i%m)
				break;
			
			else
			{
				cycle[i] += game[a];
				val[i].push_back(game[a]);
				if(cycle[i] < mincycle[i])
					mincycle[i] = cycle[i];
			}
	
			iter++;
		}
	}
	
	long long time=-1, globTime=-1;
	bool changed = false;
	for(int i=0;i<n;i++)
	{
		long long akt = players[i];
		if(cycle[i] < 0)
		{
			long long itertime = i;
			int X = ( akt + mincycle[i] ) / abs(cycle[i]); 
			if(X>0) 
			{
				itertime += (val[i].size()*n*X);
				akt+= (cycle[i]*X);
			}
			
			for(int j=0;j<val[i].size();j++)
			{
				akt += val[i][j];
				if(akt==0)
				{
					time = itertime + (j*n) + 1;
					changed = true;
					break;
				}
			}	

			if(changed)
			{
				if(globTime!=-1)
				{
					if(time < globTime)
						globTime = time;
				}
				else
					globTime = time;
			}
		}
	}
	if(changed) cout << globTime;
	else cout << -1;
			
}