Under construction

Still being built. Things may change

Candy Wall Burglary

Time limit2sMemory limit512 MB

Summary
A bandit descends and reascends through shelves connected by sparse ladders, collecting jars at most once; find the maximum candy total.
Level

Medium7 of 10

Topics
Dynamic programming, Graph, Intervals, Implementation
Solved
No attempts yet

Problem

The royal kitchen stores candy bars on one giant wall of shelves. There are NN shelves of equal length, stacked one above another from the floor up, with their left ends and their right ends lined up. Each shelf is divided into MM slots of equal size. A slot is either empty or holds one jar, and a jar holds between 1 and 9 candy bars.

Every shelf is joined to the shelf directly below it by one or more vertical ladders, and the bottommost shelf is joined to the floor the same way. A ladder joins a slot to the slot in the same column on the shelf below, or to the floor for the bottommost shelf. At most one ladder hangs directly under a given slot.

The topmost shelf holds no jars, and right above it an open window leads to the roof. A bandit came in through that window and wants to carry off as many candy bars as he can. His round trip runs like this:

  • he starts on the topmost shelf;
  • he goes down 0 or more shelves, possibly all the way to the floor;
  • then he climbs back up and leaves through the window.

Once he starts climbing back up he never goes down again. On a shelf he walks left or right into a neighboring slot, and between two shelves he uses a ladder. Entering a slot that holds a jar takes every candy bar in that jar.

The alarm is his problem. The bandit must never enter a slot that holds a jar twice, because the second entry sets off the candy alarm. Empty slots, the topmost shelf and the floor may be crossed any number of times.

Compute the largest number of candy bars the bandit can carry off without setting off the alarm.

Input

The first line has two integers NN and MM, the number of shelves and the number of slots on one shelf.

The next 2N2N lines are strings of length MM that draw the shelves and the ladders from the top down.

  • Line 2k−12k - 1 of the drawing, for 1≤k≤N1 \le k \le N, is the kk-th shelf from the top. The character - is an empty slot, and a digit xx is a jar holding xx candy bars.
  • Line 2k2k of the drawing is the row of ladders directly under the kk-th shelf from the top. The character | is a ladder, and . is bare wall.

Constraints:

  • 1≤N≤10001 \le N \le 1000
  • 1≤M≤50001 \le M \le 5000
  • The number of ladders directly under one shelf is at least 1 and at most 10.

Output

Print one integer: the largest number of candy bars the bandit can collect without setting off the alarm.

Notes

  • A slot at either end of a ladder may hold a jar.
  • A slot is entered either by walking into it along a shelf or by reaching it as the end of a ladder. Using a ladder means entering both of its end slots.
  • The topmost shelf and the floor hold no jars.
  • The window spans the whole topmost shelf, so the bandit enters at any slot of that shelf and leaves from any slot of it.
  • The floor is one continuous strip under the bottommost shelf, and the bandit walks along it freely.

Examples1

  1. Example 1

    Input
    5 20
    --------------------
    .|.....|...|......|.
    1-1--1115-3-1-1--1-1
    ....|.........|.....
    ---1-11-1-11---1--3-
    .......|.........|..
    -7----9-4-----------
    ..|.................
    --------9-----------
    .|.................|
    
    Expected output
    38