We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
  • HackerRank Home
  • |
  • Prepare
  • Certify
  • Compete
  • Apply
  • Hiring developers?
  1. Prepare
  2. Algorithms
  3. Dynamic Programming
  4. Matrix Land

Matrix Land

Problem
Submissions
Leaderboard
Discussions
Editorial

You are playing a matrix-based game with the following setup and rules:

  • You are given a matrix with rows and columns. Each cell contains some points. When a player passes a cell their score increases by the number written in that cell and the number in the cell becomes . (If the cell number is positive their score increases, otherwise it decreases.)
  • The player starts from any cell in the first row and can move left, right or down.
  • The game is over when the player reaches the last row and stops moving.

image

Print the maximum score that the player can get.

Input Format

The first line contains and . The next lines contain numbers each, number in line denotes the number that is written on cell .

Constraints

Subtasks

  • for tests .
  • for tests .

Output Format

Print the maximum score that the player can get.

Sample Input 0

4 5
1 2 3 -1 -2
-5 -8 -1 2 -150
1 2 3 -250 100
1 1 1 1 20

Sample Output 0

37

Explanation 0

Refer the image given in statement, the path followed is summing upto .
Note that, is traversed times, but the second time it only contributes to the sum.

Author

nikasvanidze

Difficulty

Hard

Max Score

55

Submitted By

1890

Need Help?


View discussions
View editorial
View top submissions

rate this challenge

MORE DETAILS

Download problem statement
Download sample test cases
Suggest Edits
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Helpdesk
  • Careers
  • Terms Of Service
  • Privacy Policy

Cookie support is required to access HackerRank

Seems like cookies are disabled on this browser, please enable them to open this website

Join us

Create a HackerRank account

Be part of a 26 million-strong community of developers

Please signup or login in order to view this challenge

or
Already have an account?Log in