 MIKE1 - Editorial

Author: Constantine Sokol

Tester: Roman Rubanenko

Editorialist: Praveen Reddy Vaka

cakewalk

None

EXPLANATION

This is a straightforward implementation problem.

You have to read the first line of the test data to get the values N and M.

Then read the next N lines (M integers per line corresponding to a row in the matrix) to read the matrix.

Store the matrix in a 2D array A.

Read the next line to get the value of L.

Maintain two variables E1 and E2 initialized to 0.

While reading the next L lines of test data and perform the following tasks

`````` Let the line read be “i j”
if E1 != -1
if A*[j] doesn’t go out of bound set E1 = E1 + A*[j]
else set E1 = -1
if E2 != -1
if A[j]* doesn’t go out of bound set E2 = E2 + A[j]*
else set E2 = -1
``````

Report max(E1, E2)

To check if A*[j] goes out of bound or not you just have to simply check if 0<=i<N and 0<=j<M. In languages like Java you can simply have your statement E1 = E1 + A*[j] inside a try catch block and if it throws ArrayOutOfBoundsException then you set E1 to -1.