<|instruction|>
Solve the below problem. The solution should start with an abridged problem statement. Then key observations. Then full solution based on the observations. Then C++ and Python implementations with comments.

346. Snooker
Time limit per test: 0.25 second(s)
Memory limit: 65536 kilobytes
input: standard
output: standard



Snooker is a cue sport that is played on a large (12 ft * 6 ft, 3.6 m * 1.8 m) baize-covered table with pockets in each of the four corners and in the middle of each of the long side cushions. It is played using a cue, one white ball (the cue ball), 15 red balls (worth 1 point each) and 6 balls of different colours (worth 2-7 points each). [Wikipedia, the free encyclopedia]

Snooker is played by two players or two teams. The main rule in snooker is to alternate potting reds and colours — that's how yellow, green, brown, blue, pink and black balls are called. They cost from 2 to 7 points each in this particular order. Alternating means that after potting a red ball, the player has to pot any colour ball and vice versa. If the player has potted any colour ball and there are still some reds or he has potted last red by the previous shot, this potted colour ball is put back on the table. If there are no reds and a colour was out back for the last time, the player has to pot all colours in the increasing order of their cost. If there is at least one red ball ath the table, the player can start his break (a sequence of continuously potted balls) only with the red ball. These rules will be enough for this problem.

For example, let's consider two reds on the table. The player may pot them in order like "red - black - red - pink - yellow - green - brown - blue - pink - black" which is correct break, but "red - red" or "red - black - pink" is not correct.

Professional players always estimate "score on the table" — maximal score one can achieve by correctly potting all the balls on the table. Obviously, this can be done by alternating reds with black, because black is the most expensive colour ball. Your task is to find this maximal score.

Input
The first line of the input file contains 7 numbers, representing the number of the balls of each color in a particular order: red, yellow, green, brown, blue, pink and black. The number of all balls except red will be from 0 to 1, the number of red balls will be from 0 to 15. The second line contains one word: "RED" if the last potted ball was red, "COLOUR" if the last potted ball was colour, and "NONE" if the next shot will be the first in the player's brake. You may assume that if there is at least one red ball, there are all colours. Also you may assume that if there are no reds, colours have been potted in the correct order.

Output
Write to the output file the desired maximum score.

Example(s)
sample input
sample output
2 1 1 1 1 1 1
COLOUR
43

sample input
sample output
2 1 1 1 1 1 1
RED
50

sample input
sample output
0 0 0 1 1 1 1
NONE
22

sample input
sample output
0 1 1 1 1 1 1
RED
34



Note
"Red - black - red - black - yellow - green - brown - blue - pink - black" costs 1 + 7 + 1 + 7 + 2 + 3 + 4 + 5 + 6 + 7 = 43 points.

"Black - red - black - red - black - yellow - green - brown - blue - pink - black" costs 7 + 1 + 7 + 1 + 7 + 2 + 3 + 4 + 5 + 6 + 7 = 50 points.

According to the rules, player has to pot brown ball next, so "brown - blue - pink - black" costs 4 + 5 + 6 + 7 = 22 points.

"Black - yellow - green - brown - blue - pink - black" costs 7 + 2 + 3 + 4 + 5 + 6 + 7 = 34 points.

<|response|>
## 1) Abridged problem statement (concise)

You are given counts of snooker balls currently on the table:

- `R` red balls (0…15), each worth **1**
- 6 colour balls: yellow..black worth **2..7**, each count is **0 or 1**

Also given the break state:

- `"NONE"`: next shot is the first shot of the break
- `"RED"`: last potted ball was red ⇒ next must be a **colour**
- `"COLOUR"`: last potted ball was a colour ⇒ next must be a **red** (if any reds exist)

Rules (only what matters here):

- While any reds remain: you must alternate **red ↔ colour**.
- Any colour potted while reds still exist (or right after potting the last red) is **re-spotted** (returns to the table).
- After reds are gone: pot remaining colours **once each** in increasing value order (2→7), no re-spotting.
- If `R > 0`, you may assume **all colours are present**.

Compute the **maximum total score** obtainable by legally potting all balls.

---

## 2) Key observations

1. **Best colour during the “reds exist” phase is always black (7).**  
   Since the colour is re-spotted, you can score **7** repeatedly.

2. Therefore, with `R` reds, the maximum from alternating is:
   \[
   R \times (1 + 7) = 8R
   \]

3. After all reds are potted, remaining colours are taken **once** (no re-spotting).  
   Their contribution is:
   \[
   \text{colour\_sum} = \sum_{v=2}^{7} (\text{present}(v) \cdot v)
   \]

4. The given `state` may force an **extra colour pot** before the normal plan:
   - If `state == "RED"`, the next ball must be a colour.
     - If reds still exist, take **black** for +7 (it will be re-spotted, so it doesn’t reduce final colour clearance).
     - If no reds exist, you can take the **highest remaining colour** once as an extra immediate score, then still clear colours.

---

## 3) Full solution approach

Let the input be `balls[0..6]` in order:
- `balls[0]` = reds
- `balls[1]..balls[6]` = yellow..black

Values mapping: `balls[i]` (for `i=1..6`) has value `i+1`.

Compute:
- `reds = balls[0]`
- `colour_sum = Σ_{i=1..6} balls[i] * (i+1)`

Then:

### Case A: `reds > 0`
- Maximum plan:
  - Alternate each red with black: score `8 * reds`
  - Then clear colours once: `+ colour_sum` (in fact equals 27 because all colours exist, but we can still compute it)
- If `state == "RED"`: must pot a colour immediately ⇒ take black for `+7`.

So:
- If `state != "RED"`: `score = 8*reds + colour_sum`
- If `state == "RED"`: `score = 8*reds + colour_sum + 7`

### Case B: `reds == 0`
- Only colours remain, so baseline score is `colour_sum`.
- If `state == "RED"`: must pot a colour next; choose the highest remaining colour value `max_colour` and add it once:
  - `score = colour_sum + max_colour` (if any colour exists; otherwise +0)

Complexity is O(1) (only 7 numbers).

---

## 4) C++ implementation (detailed comments)

```cpp
#include <bits/stdc++.h>

using namespace std;

template<typename T1, typename T2>
ostream& operator<<(ostream& out, const pair<T1, T2>& x) {
    return out << x.first << ' ' << x.second;
}

template<typename T1, typename T2>
istream& operator>>(istream& in, pair<T1, T2>& x) {
    return in >> x.first >> x.second;
}

template<typename T>
istream& operator>>(istream& in, vector<T>& a) {
    for(auto& x: a) {
        in >> x;
    }
    return in;
};

template<typename T>
ostream& operator<<(ostream& out, const vector<T>& a) {
    for(auto x: a) {
        out << x << ' ';
    }
    return out;
};

vector<int> balls;
string state;

void read() {
    balls.resize(7);
    cin >> balls >> state;
}

void solve() {
    // An important note in the statement is "You may assume that if there is at
    // least one red ball, there are all colours.", which makes this fairly
    // simple. We can greedily alternate reds with black (the most expensive
    // colour, worth 7) to maximize score. Each red-black pair scores 1+7=8
    // points. After all reds are exhausted, all the colours are potted in
    // increasing order (2+3+4+5+6+7=27). If state is RED, we first pot the best
    // available colour (which goes back on the table since the last red was the
    // previous shot), gaining extra points. With no reds, simply sum the
    // remaining colours, plus the best colour again if state is RED.

    int reds = balls[0];
    int colour_sum = 0;
    for(int i = 1; i < 7; i++) {
        colour_sum += balls[i] * (i + 1);
    }

    int score = 0;
    if(reds > 0) {
        score = reds * 8 + colour_sum;
        if(state == "RED") {
            score += 7;
        }
    } else {
        score = colour_sum;
        if(state == "RED") {
            for(int i = 6; i >= 1; i--) {
                if(balls[i]) {
                    score += i + 1;
                    break;
                }
            }
        }
    }

    cout << score << "\n";
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int T = 1;
    // cin >> T;
    for(int test = 1; test <= T; test++) {
        read();
        solve();
    }

    return 0;
}
```

---

## 5) Python implementation (detailed comments)

```python
import sys

def main() -> None:
    # Read 7 integers: red, yellow, green, brown, blue, pink, black
    balls = list(map(int, sys.stdin.readline().split()))
    state = sys.stdin.readline().strip()

    reds = balls[0]

    # Sum of remaining colours when finally cleared (each colour worth 2..7)
    # balls[1] -> 2, ..., balls[6] -> 7
    colour_sum = 0
    for i in range(1, 7):
        colour_sum += balls[i] * (i + 1)

    if reds > 0:
        # Optimal: (red + black) repeated for each red => 8 points per red
        score = 8 * reds + colour_sum

        if state == "RED":
            # Must pot a colour next; best is black (+7). It is re-spotted while reds exist.
            score += 7
    else:
        # No reds: just clear remaining colours (sum of present values)
        score = colour_sum

        if state == "RED":
            # Must pot a colour next; choose the highest remaining colour for extra points
            for i in range(6, 0, -1):  # 6..1 corresponds to values 7..2
                if balls[i] > 0:
                    score += (i + 1)
                    break

    print(score)

if __name__ == "__main__":
    main()
```
