Under construction

Still being built. Things may change

Naming the Competition

Time limit2sMemory limit1024 MB

Summary
For each interval of cards, compare the best alternating 연고 and 고연 subsequence scores of Y and K, then print the winner and its score.
Level

Medium7 of 10

Topics
Segment tree, Dynamic programming, String
Solved
No attempts yet

문제

Yonsei University and Korea University hold an annual rivalry match, and a rule decides its official name. The host school is placed last. When Yonsei hosts, the official name is "Goyeonjeon". When Korea University hosts, it is "Yeongojeon". Regardless of the rule, Yonsei students usually call it "Yeongojeon" and Korea University students call it "Goyeonjeon". When the staff of each school discussed the official name of this competition, each wanted their own school name first. They decided to settle it with the following game. Y, the problem setter from Yonsei, played against K, the problem setter from Korea University.

There are NN cards in a row. Each card has either '연' or '고' written on it, along with a natural number at most 10910^9. Number the cards from the left as card 1, card 2, ..., card NN. From the cards between ll and rr inclusive, pick some cards in order. Y picks cards so that the pattern "연고" repeats, and K picks cards so that the pattern "고연" repeats. Two consecutively picked cards cannot have the same letter. Y's first card has '연' and its last card has '고'. K's first card has '고' and its last card has '연'. The sum of the numbers on the cards each player picks is that player's score, and the higher score wins. If a player cannot pick any cards, that player's score is 0.

This game is played TT times. In each game, Y and K are allowed to pick the same card. Y and K play each game with their school pride at stake, using their best strategy. For each game, print the winner and the winner's score.

입력

The first line contains the number of cards NN.

The second line contains the letters on the cards as a single string. A card with '연' is written as Y, and a card with '고' is written as K.

The third line contains the natural numbers aia_i on each card, separated by spaces. (1≤i≤N1 \leq i \leq N)

The fourth line contains the number of games TT.

From the fifth line onward, the TT lines each contain an interval [li,ri][l_i, r_i] from which cards may be picked. (1≤i≤T1 \leq i \leq T)

출력

For each game, print the winner and the winner's score, separated by a space. If the game is a tie, print YK and the score, separated by a space.

제한

1≤N,T≤500 0001 \leq N, T \leq 500\,000

1≤ai≤1091 \leq a_i \leq 10^9 (1≤i≤N)(1 \leq i \leq N)

1≤li≤ri≤N1 \leq l_i \leq r_i \leq N (1≤i≤T)(1 \leq i \leq T)

힌트

The official name of this competition is "2023 Korea University x Yonsei University Programming Contest".

Examples1

  1. Example 1

    Input
    7
    YYKKYYK
    2 4 1 2 3 4 5
    4
    1 7
    3 5
    5 6
    2 6
    
    Expected output
    Y 15
    K 5
    YK 0
    YK 6