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
#include <cstdlib>
#include <stdio.h>
#include <iostream>

#include <vector>
#include <set>
#include <map>
#include <queue>
#include <algorithm>

unsigned n,m;
std::vector<int> players;
std::vector<int> players_kopia;
std::vector<short> cykl;

bool Progress()
{
	for(int j=0;j<n;j++)
	 if (players[j]<players_kopia[j])
		 return true;
	 return false;
}

int main()
{
	std::ios_base::sync_with_stdio(0);
	
	
    std::cin >> n;
	
	int p;
	for(int i=0;i<n;i++)
	{
		std::cin >>p;
		players.push_back(p);
		players_kopia.push_back(p);
	}

	std::cin >> m;

	char c;
	for(int i=0;i<m;i++)
	{
		std::cin >>c;
		if (c=='W')
			cykl.push_back(1);
		else
			cykl.push_back(-1);
	}

	bool run = true;

	int pl_pos=0, cykl_pos=0;
	long numer_testu=1;
	while(run)
	{
		players[pl_pos] += cykl[cykl_pos];
		if (players[pl_pos]==0)
		{
			std::cout << numer_testu << std::endl;
			return 0;
		}

		if (numer_testu==n*m)
		{
			if (!Progress())
				run = false;
		}

		numer_testu++;
		pl_pos++;
		cykl_pos++;
		
		if (pl_pos==n)
			pl_pos=0;

		if (cykl_pos==m)
			cykl_pos=0;

	}

	std::cout << "-1" << std::endl;

	return 0;
}