## 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 (explaining the solution)

### 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) Provided C++ solution with detailed line-by-line comments

```cpp
#include <bits/stdc++.h>              // Includes almost all standard headers (common CP shortcut)
using namespace std;

// Pretty-print a pair as "first second"
template<typename T1, typename T2>
ostream& operator<<(ostream& out, const pair<T1, T2>& x) {
    return out << x.first << ' ' << x.second;
}

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

// Read a vector: reads each element in order
template<typename T>
istream& operator>>(istream& in, vector<T>& a) {
    for(auto& x: a) {                 // iterate by reference to fill elements
        in >> x;
    }
    return in;
};

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

vector<int> balls;                    // balls[0]=red, balls[1]=yellow, ..., balls[6]=black
string state;                         // "RED", "COLOUR", or "NONE"

void read() {
    balls.resize(7);                  // ensure vector has 7 slots
    cin >> balls >> state;            // read 7 integers and then the state string
}

void solve() {
    // The statement says: if there is at least one red, then all colours exist.
    // That allows a simple optimal strategy:
    // - While reds exist: alternate red with black (7 points) to maximize score.
    //   Each red-black pair scores 1 + 7 = 8, repeated for each red.
    // - After reds are gone: pot all colours once in increasing value order.
    // - If state is RED: we must pot a colour next; choose the best available (black if possible).
    //   When reds existed, that extra colour is effectively an extra +7 before starting the red/black pairs.
    //   When no reds, it is +value of best remaining colour.

    int reds = balls[0];              // number of red balls

    int colour_sum = 0;               // total points from remaining colours when finally cleared
    for(int i = 1; i < 7; i++) {      // i=1..6 correspond to values 2..7
        colour_sum += balls[i] * (i + 1); // add value*(presence count)
    }

    int score = 0;

    if(reds > 0) {
        // Best play: for each red, take black after it => 8 points per red.
        // Then clear all colours once => +colour_sum (which is 27 if all colours exist).
        score = reds * 8 + colour_sum;

        if(state == "RED") {
            // If last potted ball was red, next must be a colour.
            // To maximize, pot black now (it will be re-spotted while reds exist).
            score += 7;
        }
    } else {
        // No reds: only colours remain, so we just collect their sum.
        score = colour_sum;

        if(state == "RED") {
            // If last was red, next must be a colour even though no reds remain.
            // We can choose the highest remaining colour for maximum extra points.
            for(int i = 6; i >= 1; i--) { // scan from black down to yellow
                if(balls[i]) {            // if this colour is present
                    score += i + 1;       // add its value (i+1)
                    break;                // only one extra colour is potted here
                }
            }
        }
    }

    cout << score << "\n";            // output answer
}

int main() {
    ios_base::sync_with_stdio(false); // faster I/O
    cin.tie(nullptr);                 // untie cin/cout for speed

    int T = 1;                        // single test case
    // cin >> T;                       // (disabled) could be used if multiple tests existed
    for(int test = 1; test <= T; test++) {
        read();                       // read input
        solve();                      // compute and print result
    }

    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).