This page is still under construction.

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

Little Shop of Flowers

Interview

Time limit1sMemory limit128 MB

Summary
Place F ordered bunches into a row of V vases to maximize total aesthetic value, then output the lexicographically smallest optimal assignment.
Level

Medium6 of 10

Topics
Dynamic programming
Solved
No attempts yet

Problem

You want to arrange the window of your flower shop as pleasantly as possible. You have FF bunches of flowers, each of a different kind, and a row of at least FF vases. The vases are fixed to the shelf and numbered 11 through VV from left to right, so vase 11 is the leftmost and vase VV is the rightmost. The bunches are movable and are identified by the integers 11 through FF.

These id-numbers fix the required left-to-right order: whenever i<ji < j, bunch ii must stand in a vase to the left of the vase holding bunch jj. For example, with a bunch of azaleas (id 11), begonias (id 22), and carnations (id 33), the azaleas must be left of the begonias, and the begonias must be left of the carnations. If there are more vases than bunches, the extra vases are left empty. Each vase holds at most one bunch.

Every vase has its own character, so placing a particular bunch in a particular vase yields an aesthetic value, given as an integer. Let Ai,jA_{i,j} be the aesthetic value of putting bunch ii into vase jj. Leaving a vase empty contributes 00.

For example, the aesthetic values might be:

Vase 1Vase 2Vase 3Vase 4Vase 5
1 (Azaleas)723-5-2416
2 (Begonias)521-41023
3 (Carnations)-215-4-2020

Here azaleas look great in vase 2 but awful in vase 4.

Place every bunch so that the required order is respected and the total aesthetic value is as large as possible.

Input

  • The first line contains two integers FF and VV.
  • Each of the next FF lines contains VV integers; the jj-th integer on the (i+1)(i+1)-th line is Ai,jA_{i,j}, the aesthetic value of placing bunch ii into vase jj.

Output

  • On the first line, print the maximum possible total aesthetic value.
  • On the second line, print FF integers: the kk-th integer is the vase that holds bunch kk in an arrangement achieving that maximum. If several arrangements reach the maximum, print the lexicographically smallest one — comparing the sequences of vase numbers for bunches 1,2,…,F1, 2, \dots, F, the sequence that is smaller at the first position where they differ.

Constraints

  • 1≤F≤1001 \le F \le 100, where FF is the number of bunches (numbered 11 through FF).
  • F≤V≤100F \le V \le 100, where VV is the number of vases.
  • −50≤Ai,j≤50-50 \le A_{i,j} \le 50, where Ai,jA_{i,j} is the aesthetic value of putting bunch ii into vase jj.

Examples4

  1. Example 1

    Input
    3 5
    7 23 -5 -24 16
    5 21 -4 10 23
    -21 5 -4 -20 20
    
    Expected output
    53
    2 4 5
    
  2. Example 2

    Input
    1 1
    5
    
    Expected output
    5
    1
    
  3. Example 3

    Input
    1 5
    -3 -1 -1 -5 -2
    
    Expected output
    -1
    2
    
  4. Example 4

    Input
    2 2
    1 2
    3 4
    
    Expected output
    5
    1 2