This page is still under construction.

Parts of this page are still being built. What you see may change.

Go Endgame

Time limit1sMemory limit128 MB

Summary
Given starting scores, region values, and sente flags, compute the final scores when Alice and Bob alternately pick regions and respond until all are settled.
Level

Hard8 of 10

Topics
Dynamic programming, Game theory, Greedy, Sorting
Solved
No attempts yet

Problem

Go is a board game played on a grid. The goal is to surround as much territory as possible with stones of your own color. Playing a full game of Go is extremely hard for computers, but the endgame — the stage where the borders of the territories are almost settled and the players only squeeze out the last few points — can be captured by a simplified model. This task studies such a simplified endgame model.

The game is played by Alice and Bob, who alternate turns. Alice currently has aa points of territory and Bob has bb points. There are nn separate regions, and a move inside one region never affects any other region. It is Alice's turn.

Play proceeds as follows. The player on turn chooses a region and plays in it; the opponent responds in the same region, and they keep responding to each other until that region is settled. Whoever is on turn afterward chooses the next region, and so on, until every region is settled.

The player who starts a region has an advantage there and usually gains more points. We model this per region: if Alice starts region ii she gains aia_i points and Bob gains nothing in it; if Bob starts region ii he gains bib_i points and Alice gains nothing in it.

A region may also be sente for a player. If a region is sente for the player who starts it, that same player is still on turn after the region is settled (so they also choose the next region). If the region is not sente for the player who starts it, the opponent is on turn once it is settled. A region can be sente for both players, for only one of them, or for neither.

Given the description of all regions, determine the final score assuming both players play optimally. Each player wants their own score to exceed the opponent's by as much as possible (or to trail by as little as possible); among lines of play that give the same score difference, each player prefers the one in which their own score is as large as possible.

Input

The input contains several instances, separated by single blank lines.

The first line of an instance contains three integers aa, bb, and nn (0≤a,b0 \le a, b, a+b≤361a + b \le 361, and 0≤n≤3610 \le n \le 361): Alice's current points, Bob's current points, and the number of unsettled regions.

Each of the next nn lines describes one region. The ii-th such line contains two integers aia_i and bib_i (0≤ai,bi0 \le a_i, b_i and 1≤ai+bi≤3611 \le a_i + b_i \le 361) and two characters sis_i and tit_i, separated by single spaces. Here aia_i and bib_i are the points Alice and Bob gain by starting that region. The character sis_i is S if the region is sente for Alice and G otherwise; likewise tit_i is S if the region is sente for Bob and G otherwise.

Process instances until the end of input.

Output

For each instance, print a single line with two integers AA and BB separated by one space: the final scores of Alice and Bob under optimal play, with Alice on turn at the start. Every instance satisfies A+B≤361A + B \le 361.

Examples3

  1. Example 1

    Input
    0 0 1
    5 6 G S
    
    10 9 3
    2 10 G G
    1 1 S G
    8 6 G S
    
    Expected output
    5 0
    19 19
    
  2. Example 2

    Input
    0 0 3
    3 5 S S
    2 2 S S
    4 1 S S
    
    Expected output
    9 0
    
  3. Example 3

    Input
    0 0 2
    3 3 G G
    4 2 G G
    
    Expected output
    4 3