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

104. Little shop of flowers

time limit per test: 0.25 sec.
memory limit per test: 4096 KB



PROBLEM

You want to arrange the window of your flower shop in a most pleasant way. You have F bunches of flowers, each being of a different kind, and at least as many vases ordered in a row. The vases are glued onto the shelf and are numbered consecutively 1 through V, where V is the number of vases, from left to right so that the vase 1 is the leftmost, and the vase V is the rightmost vase. The bunches are moveable and are uniquely identified by integers between 1 and F. These id-numbers have a significance: They determine the required order of appearance of the flower bunches in the row of vases so that the bunch i must be in a vase to the left of the vase containing bunch j whenever i < j. Suppose, for example, you have bunch of azaleas (id-number=1), a bunch of begonias (id-number=2) and a bunch of carnations (id-number=3). Now, all the bunches must be put into the vases keeping their id-numbers in order. The bunch of azaleas must be in a vase to the left of begonias, and the bunch of begonias must be in a vase to the left of carnations. If there are more vases than bunches of flowers then the excess will be left empty. A vase can hold only one bunch of flowers.

Each vase has a distinct characteristic (just like flowers do). Hence, putting a bunch of flowers in a vase results in a certain aesthetic value, expressed by an integer. The aesthetic values are presented in a table. Leaving a vase empty has an aesthetic value of 0.

To achieve the most pleasant effect you have to maximize the sum of aesthetic values for the arrangement while keeping the required ordering of the flowers. If more than one arrangement has the maximal sum value, any one of them will be acceptable. You have to produce exactly one arrangement.

ASSUMPTIONS

1 ≤ F ≤ 100 where F is the number of the bunches of flowers. The bunches are numbered 1 through F.

F ≤ V ≤ 100 where V is the number of vases.

-50 ≤ Aij ≤ 50 where Aij is the aesthetic value obtained by putting the flower bunch i into the vase j.

Input


The first line contains two numbers: F, V.

The following F lines: Each of these lines contains V integers, so that Aij is given as the j’th number on the (i+1)’st line of the input file.

Output


The first line will contain the sum of aesthetic values for your arrangement.

The second line must present the arrangement as a list of F numbers, so that the k’th number on this line identifies the vase in which the bunch k is put.
Sample Input

3 5
7 23 -5 -24 16
5 21 -4 10 23
-21 5 -4 -20 20
Sample Output

53
2 4 5

<|response|>
1. Abridged Problem Statement
You have F flowers (numbered 1…F) and V ≥ F vases in a row (numbered 1…V). Placing flower i in vase j yields an aesthetic score A[i][j]; leaving a vase empty gives 0. You must place each flower in exactly one vase so that the vase indices strictly increase with flower indices (flower 1 in a vase left of flower 2’s vase, etc.). Maximize the total aesthetic score and output the maximum sum and one valid assignment of flowers to vases.

2. Key Observations
- The ordering constraint (flower indices and vase indices both increasing) means we pick an increasing sequence of vase positions of length F.
- This is equivalent to a DP over two indices: which flower we’re placing next, and which vase we’re considering.
- At each state (i, j), we decide to either skip vase j (leave it empty) or place flower i there, then move on.
- There are only O(F·V) states and each transition is O(1), so the total time is O(F·V), which is fine for F,V ≤ 100.

3. Full Solution Approach
Let dp[i][j] = maximum total aesthetic score when we need to place flowers i…F-1 and can use vases j…V-1 (0-based).

Base Cases:
- dp[i][j] = 0 when i == F (no flowers left to place).
- dp[i][j] = −∞ when j == V but i < F (no vases left but flowers remain → impossible).

Recurrence for 0 ≤ i < F, 0 ≤ j < V:
- Option 1 (skip vase j): score = dp[i][j+1].
- Option 2 (use vase j for flower i): score = A[i][j] + dp[i+1][j+1].
Take dp[i][j] = max(option1, option2).

The reference solution computes dp with a top-down memoized recursion, storing in best[i][j] which option was optimal (0 = skip, 1 = take).

To reconstruct one optimal assignment, start at i=0, j=0 and repeat until i=F:
- If best[i][j] == 1, assign flower i to vase j, then i++ and j++.
- Otherwise skip this vase: j++.

Finally output dp[0][0] and the chosen vase indices (convert from 0-based to 1-based).

4. C++ Implementation
```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;
};

int n, m;
vector<vector<int>> a;

void read() {
    cin >> n >> m;
    a.assign(n, vector<int>(m));
    cin >> a;
}

vector<vector<int>> dp;
vector<vector<int>> best;

int rec(int i, int j) {
    if(i == n) {
        return 0;
    }
    if(j == m) {
        return -1e9;
    }

    int& memo = dp[i][j];
    if(memo != -1) {
        return memo;
    }

    best[i][j] = 0;
    memo = rec(i, j + 1);

    int take = a[i][j] + rec(i + 1, j + 1);
    if(take > memo) {
        memo = take;
        best[i][j] = 1;
    }

    return memo;
}

void solve() {
    // - dp[i][j] = best total aesthetic value placing bunches i..F-1 into vases
    //   j..V-1, preserving the order (bunch i must sit left of bunch i+1).
    //
    // - At each state we either skip vase j (move to dp[i][j+1]) or place bunch
    //   i in vase j and take a[i][j] + dp[i+1][j+1]; best[i][j] records which
    //   choice was optimal so the placement can be reconstructed.
    //
    // - Walk the best[] choices from (0, 0) to read off the chosen vase for
    //   each bunch.

    dp.assign(n, vector<int>(m, -1));
    best.assign(n, vector<int>(m, 0));

    vector<int> ans(n);
    cout << rec(0, 0) << '\n';

    int i = 0, j = 0;
    while(i != n) {
        rec(i, j);
        if(best[i][j] == 0) {
            j++;
        } else {
            ans[i] = j;
            i++;
            j++;
        }
    }

    for(int k = 0; k < n; k++) {
        cout << ans[k] + 1 << " ";
    }
    cout << '\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();
        // cout << "Case #" << test << ": ";
        solve();
    }

    return 0;
}
```

5. Python Implementation with Detailed Comments
```python
import sys
sys.setrecursionlimit(10000)

def main():
    # Read number of flowers n and vases m
    n, m = map(int, sys.stdin.readline().split())
    # Read the aesthetic scores: a[i][j]
    a = [list(map(int, sys.stdin.readline().split())) for _ in range(n)]

    # dp[i][j] = best score placing flowers i..n-1 into vases j..m-1
    dp = [[None] * (m + 1) for _ in range(n + 1)]
    # best_dec[i][j] = True if optimal at (i,j) is to take, False to skip
    best_dec = [[False] * (m + 1) for _ in range(n + 1)]

    NEG_INF = -10**9

    def rec(i, j):
        # Base: all flowers placed
        if i == n:
            return 0
        # No vases left but still flowers => impossible
        if j == m:
            return NEG_INF
        if dp[i][j] is not None:
            return dp[i][j]

        # Option 1: skip vase j
        skip = rec(i, j + 1)
        take = a[i][j] + rec(i + 1, j + 1)

        # Choose better
        if take > skip:
            dp[i][j] = take
            best_dec[i][j] = True
        else:
            dp[i][j] = skip
            best_dec[i][j] = False
        return dp[i][j]

    # Compute answer
    max_score = rec(0, 0)
    print(max_score)

    # Reconstruct chosen vases
    res = []
    i = j = 0
    while i < n and j < m:
        if best_dec[i][j]:
            res.append(j + 1)  # store 1-based index
            i += 1
            j += 1
        else:
            j += 1

    print(" ".join(map(str, res)))

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