This page is still under construction.

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

Collecting Apples

Time limit1sMemory limit512 MB

Summary
Find the Kth best monotone path from (1,1) to (R,C) in a grid, ranking paths by total apple count then by lexicographic order of the move string.
Level

Medium7 of 10

Topics
Dynamic programming, Combinatorics, Greedy, Sorting
Solved
No attempts yet

Problem

Santi is playing a game on a grid with RR rows (from top to bottom) and CC columns (from left to right). The cell in the iith row and jjth column is called cell (i,j)(i, j). Cell (i,j)(i, j) contains Ai,jA_{i,j} apples.

Santi's character Nano starts on cell (1,1)(1, 1) and wants to move to cell (R,C)(R, C). On each turn Nano can move only one cell down or one cell right, which brings it closer to the goal. Nano's path to the goal can be written as a string of R+C−2R + C - 2 characters, of which R−1R - 1 are ‘D’ and C−1C - 1 are ‘R’. This means that Nano moves one cell down on the iith turn if the iith character is ‘D’, and one cell right on the iith turn if the iith character is ‘R’.

Two paths are different if their string representations are different. There are (R+C−2R−1)R+C-2 \choose R-1 different paths. Path XX is better than path YY if either of the following holds:

  • Path XX collects more apples than YY. The number of apples Nano collects along a path is the total number of apples in the cells the path passes through, including cell (1,1)(1, 1) and cell (R,C)(R, C).
  • Path XX collects the same number of apples as YY, and the string representation of XX is lexicographically smaller than the string representation of YY.

Finding the best path is too boring for Santi, so she wants to find the KKth best path instead. In other words, she wants to find a path XX such that K−1K - 1 paths are better than XX and (R+C−2R−1)−K{R+C-2 \choose R-1} - K paths are worse than XX.

For example, let R=2R = 2, C=3C = 3, K=3K = 3, A1,1..3={1,2,3}A_{1,1..3} = \{1, 2, 3\}, and A2,1..3={2,2,3}A_{2,1..3} = \{2, 2, 3\}. Then there are three different paths to the goal, listed from best to worst.

  1. The path represented by the string RRD. It collects 1+2+3+3=91 + 2 + 3 + 3 = 9 apples.
  2. The path represented by the string DRR. It collects 1+2+2+3=81 + 2 + 2 + 3 = 8 apples.
  3. The path represented by the string RDR. It collects 1+2+2+3=81 + 2 + 2 + 3 = 8 apples.

Therefore, the 33rd best path is the path represented by the string RDR.

Input

The first line contains three integers RR CC KK (2≤R,C≤30;1≤K≤(R+C−2R−1)2 \le R, C \le 30; 1 \le K \le {R+C-2 \choose R-1}), which are the number of rows, the number of columns, and the index of the path Santi wants to find. The next RR lines each contain CC integers giving the number of apples in each cell. The jjth integer on the iith line is Ai,jA_{i,j} (0≤Ai,j≤2000 \le A_{i,j} \le 200).

Output

Print one line containing the string that represents the KKth best path to the goal.

Examples2

  1. Example 1

    Input
    2 3 3
    1 2 3
    2 2 3
    
    Expected output
    RDR
    
  2. Example 2

    Input
    3 3 5
    1 1 1
    1 1 1
    1 1 1
    
    Expected output
    RDRD