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
import java.util.Scanner;

public class haz {

    static int value(char[] pattern) {
        int value = 0;
        for (int i=0; i<pattern.length; i++) {
            if (pattern[i] == 'W') ++value;
            else --value;
        }

        return value;
    }

    static boolean onlyP(char[] pattern) {
        for (int i=0; i<pattern.length; i++) {
            if (pattern[i] != 'P') return false;
        }

        return true;
    }

    static boolean onlyW(char[] pattern) {
        for (int i=0; i<pattern.length; i++) {
            if (pattern[i] != 'W') return false;
        }

        return true;
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int[] A = new int[n];

        int minValue = Integer.MAX_VALUE;
        for (int i=0; i<n; i++) {
            A[i] = in.nextInt();
            if (A[i] < minValue) minValue = A[i];
        }

        int m = in.nextInt();

        char[] pattern = in.next().toCharArray();

        if (onlyP(pattern)) {
            System.out.println(minValue);
        } else if (onlyW(pattern)) {
            System.out.println(-1);
        } else if (n == m) {
            int minPIdx = -1;
            for (int i=0; i<m; i++) {
                if (pattern[i] == 'P') {
                    if (minPIdx == -1 || A[minPIdx] > A[i]) minPIdx = i;
                }
            }
            System.out.println(A[minPIdx]);
        } else {
            int patternIdx  = 0;
            int moneyIdx = 0;
            long games = 0;
            int turnamentMinValue = minValue;
            while (true) {
                ++games;
                if (pattern[patternIdx] == 'W') {
                    ++A[moneyIdx];
                } else {
                    --A[moneyIdx];
                    if (A[moneyIdx] < turnamentMinValue) turnamentMinValue = A[moneyIdx];
                    if (A[moneyIdx] == 0) {
                        break;
                    }
                }

                ++patternIdx;
                ++moneyIdx;
                if (patternIdx == m) {
                    patternIdx = 0;
                    if (turnamentMinValue > minValue) {
                        games = -1;
                        break;
                    }
                }
                if (moneyIdx == n) {
                    moneyIdx = 0;
                }
            }

            System.out.println(games);
        }
    }
}