## 1) Concise abridged problem statement

You are given counts of snooker balls on the table: **red** (0–15) and each **colour** (yellow..black, each 0–1). You are also given the state of the break:

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

Rules (simplified):
- While reds exist, you must alternate **red ↔ colour**.
- After potting a colour while reds still exist (or right after the last red), that colour is **re-spotted** (returns), so you can potentially pot it again later.
- When **no reds remain**, you must pot remaining colours **in increasing value order** (2..7), and they do **not** return.
- If there is at least one red, you may assume **all colours are present** (each count = 1).

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

---

## 2) Detailed Editorial

### Key observations

**Ball values:**
- red = 1
- yellow..black = 2,3,4,5,6,7

**Two phases in snooker scoring:**
1. **Red/colour alternation phase (while reds exist)**
   To maximize score, every time you must pot a colour, you should choose the **highest value available**, i.e., **black (7)**.
   Because colours are re-spotted while reds exist, you can reuse black for every such colour shot.

   So each `(red + best_colour)` pair yields:
   \[
   1 + 7 = 8
   \]
   If there are `R` reds, you can score `R * 8` from potting all reds with an optimal colour after each.

2. **Colour clearance phase (after all reds are gone)**
   Now colours are potted once each in fixed order (2..7). Their total is:
   \[
   2+3+4+5+6+7 = 27
   \]
   But in the general input, some colours may already be missing (0/1). The total remaining colours score is simply:
   \[
   \sum_{i=2}^{7} (\text{present}(i) \cdot i)
   \]

### Handling the given "state" (what must be potted next)

- If there is **at least one red on the table**, the statement guarantees **all colours exist** (so colours sum is always 27).
- If `state == "RED"`, it means the previous ball potted was a red, so the next pot must be a **colour**.
  Importantly, that colour (since reds still exist or the last red was just potted) will be **re-spotted**, so potting an extra **black** first is optimal and adds **+7**.

So:

#### Case A: `reds > 0`
- Base best score: `reds * 8 + sum(colours remaining)`
  (Because after finishing red/black alternation, you still must clear the colours once.)
- If `state == "RED"`, you get an **extra colour before the first red**, best is black: **+7**.

So:
- if `state == "COLOUR"` or `"NONE"`:
  \[
  \text{score} = 8R + \text{colour\_sum}
  \]
- if `state == "RED"`:
  \[
  \text{score} = 8R + \text{colour\_sum} + 7
  \]

#### Case B: `reds == 0`
Now you are in the colour-clearance phase already (and input guarantees the remaining colours are consistent with having been potted in order).

- If `state != "RED"` (i.e., `"NONE"` or `"COLOUR"`), you just pot remaining colours in order; maximal score is simply `colour_sum`.
- If `state == "RED"`, you must pot a **colour next**, and since there are no reds, that colour will be the **last re-spotted colour** (you can choose any remaining colour to maximize immediate gain). Best is the **highest remaining colour**. That gives you `+max_remaining_colour`, then you still clear all remaining colours for `colour_sum`.

So:
\[
\text{score} = \text{colour\_sum} + \max\{i \mid \text{colour i present}\}
\]
(if any colour exists; otherwise +0, but constraints/examples imply some remain in such a state.)

### Complexity
Only 7 numbers; solution is O(1).

---

## 3) C++ Solution

```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;
}
```

---

## 4) Python solution (same logic) with 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 they are finally cleared (each at most 1 in input)
    # Index mapping: balls[1] is yellow worth 2, ..., balls[6] is black worth 7
    colour_sum = 0
    for i in range(1, 7):
        colour_sum += balls[i] * (i + 1)

    if reds > 0:
        # While reds exist, optimal play is to alternate each red with a black:
        # each (red+black) gives 1+7 = 8 points.
        score = reds * 8 + colour_sum

        # If the last potted ball was red, next must be a colour.
        # Best is black (7). With reds existing, that colour is re-spotted,
        # so it doesn't reduce the later final colour clearance.
        if state == "RED":
            score += 7
    else:
        # No reds: we are in the final colour-clearance situation
        score = colour_sum

        # If last was red, we must pot a colour next.
        # With no reds, we can choose the best remaining colour to maximize points.
        if state == "RED":
            # Find highest value colour still present.
            for i in range(6, 0, -1):  # black down to yellow
                if balls[i] > 0:
                    score += (i + 1)
                    break

    print(score)

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

---

## 5) Compressed editorial

- Compute `colour_sum = Σ balls[i]*(i+1)` for i=1..6 (values 2..7).
- If `reds > 0`:
  - Best alternation is always `red + black`, so `8*reds`.
  - Add `colour_sum` for final clearance.
  - If `state == "RED"`, add extra `+7` (pot black next).
- If `reds == 0`:
  - Answer starts as `colour_sum`.
  - If `state == "RED"`, add the highest remaining colour value (max i+1 with balls[i]=1).
