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
174
#define NDEBUG

#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <cassert>

using namespace std;
using ll = long long;

struct EndCondition
{
	int MoneyAtBeg;
	int CyclePos;
};

#define MAXX 1000001
int n, m, dCycle;
int Money[MAXX];
char Buff[MAXX];
int Gain[MAXX];
int Prev[MAXX], Root[MAXX], PartSum[MAXX], IndexAtSubcycle[MAXX];
int SubcycleSum[MAXX], SubcycleGamesCount[MAXX];

bool operator < (EndCondition ec1, EndCondition ec2)
{
	return ec1.MoneyAtBeg == ec2.MoneyAtBeg
		? IndexAtSubcycle[ec1.CyclePos] < IndexAtSubcycle[ec2.CyclePos]
		: ec1.MoneyAtBeg < ec2.MoneyAtBeg;
}
vector<EndCondition> EndConditionsForRoot[MAXX];

void Read();
void MakeSubcycles();
void CalcAndWriteEndTime();

int main()
{
	Read();
	MakeSubcycles();
	CalcAndWriteEndTime();
	return 0;
}

void Read()
{
	scanf("%d", &n);
	for(int i = 1; i <= n; ++i)
		scanf("%d", &Money[i]);
	scanf("%d", &m);
	scanf("%s", Buff+1);
	for(int i = 1; i <= m; ++i)
	{
		assert(Buff[i] == 'W' || Buff[i] == 'P');
		Gain[i] = Buff[i] == 'W' ? 1 : -1;
	}
}

int FixedMod(int x, int mod);
void MakeSubcycles()
{
	dCycle = FixedMod(n, m);
	for(int i = 1; i <= m; ++i)
	{
		if(Root[i] == 0)
		{
			auto root = i;
			Root[root] = root;
			auto indexAtSubcycle = 0;
			IndexAtSubcycle[root] = ++indexAtSubcycle;
			auto& EndConditions = EndConditionsForRoot[root];
			PartSum[root] = Gain[root];
			assert(Gain[root] == -1 || Gain[root] == 1);
			if(Gain[root] == -1)
				EndConditions.push_back({1, root});
			
			auto prev = root;
			while(true)
			{
				auto curr = FixedMod(prev + dCycle, m);
				if(Root[curr])
				{
					assert(Root[curr] == root);
					break;
				}
				Root[curr] = root;
				IndexAtSubcycle[curr] = ++indexAtSubcycle;
				Prev[curr] = prev;
				PartSum[curr] = PartSum[prev] + Gain[curr];
				assert(Gain[curr] == -1 || Gain[curr] == 1);
				if(Gain[curr] == -1)
					EndConditions.push_back({-PartSum[curr], curr});
				
				prev = curr;
			}
			SubcycleSum[root] = PartSum[prev];
			SubcycleGamesCount[root] = IndexAtSubcycle[prev];
			sort(EndConditions.begin(), EndConditions.end());
		}
	}
}

inline int FixedMod(int x, int mod)
{
	auto result = x % mod;
	return result == 0
		? mod
		: result;
}

ll EndTimeForBoy(int boy);
void CalcAndWriteEndTime()
{
	ll endTime = -1;
	for(int i = 1; i <= n; ++i)
	{
		auto endTimeForCurrBoy = EndTimeForBoy(i);
		if(endTimeForCurrBoy == -1)
			continue;
		if(endTime == -1 || endTimeForCurrBoy < endTime)
			endTime = endTimeForCurrBoy;
	}
	printf("%lld", endTime);
}

ll EndTimeForBoy(int boy)
{
	auto firstGame = FixedMod(boy, m);
	assert(firstGame != 0);
	auto root = Root[firstGame];
	assert(root != 0);
	auto& EndConditions = EndConditionsForRoot[root];
	if(EndConditions.empty()) return -1;	
	auto subcycleSum = SubcycleSum[root];
	auto subcycleGamesCount = SubcycleGamesCount[root];
	ll endTime = (ll)boy;
	auto money = Money[boy];
	if(firstGame != root)
	{
		auto fixedMoney = money - PartSum[Prev[firstGame]];
		auto endGame = lower_bound(EndConditions.begin(), EndConditions.end(), EndCondition{fixedMoney, firstGame});
		if(endGame != EndConditions.end() && IndexAtSubcycle[firstGame] <= IndexAtSubcycle[endGame->CyclePos])
		{
			assert(endGame->MoneyAtBeg == fixedMoney);
			auto playedGamesSub1 = IndexAtSubcycle[endGame->CyclePos] - IndexAtSubcycle[firstGame];
			return ((ll)n * playedGamesSub1) + boy;
		}
		money += subcycleSum - PartSum[Prev[firstGame]];
		endTime += (ll)(subcycleGamesCount - IndexAtSubcycle[firstGame] + 1) * (ll)n;
	}
	
	auto moneyToPassSubcycle = EndConditions.back().MoneyAtBeg + 1;
	if(money >= moneyToPassSubcycle && subcycleSum >= 0) return -1;
	if(money >= moneyToPassSubcycle && subcycleSum < 0)
	{
		auto moneyForPassedSubcycles = money - moneyToPassSubcycle;
		auto passedSubcycles = (moneyForPassedSubcycles / -subcycleSum) + 1;
		if(passedSubcycles > 0)
		{
			money += passedSubcycles * subcycleSum;
			endTime += (ll)passedSubcycles * (ll)subcycleGamesCount * (ll)n;
		}
	}

	auto endGame = lower_bound(EndConditions.begin(), EndConditions.end(), EndCondition{money, root});
	assert(endGame != EndConditions.end());
	assert(endGame->MoneyAtBeg == money);
	assert(endGame == EndConditions.begin() || endGame->MoneyAtBeg == (endGame-1)->MoneyAtBeg + 1);
	auto lastGame = endGame->CyclePos;
	auto playedGamesAtLastSubcycleSub1 = IndexAtSubcycle[lastGame] - 1;
	endTime += (ll)playedGamesAtLastSubcycleSub1 * (ll)n;
	return endTime;
}