304 North Cardinal St.
Dorchester Center, MA 02124

# Mike and Matrices CodeChef Solution

## Mike and Matrices CodeChef Solution in C++14

``````#include <bits/stdc++.h>

using namespace std;

int main() {
int n, m;
cin >> n >> m;
int a[n][m];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cin >> a[i][j];
}
}

long long s1 = 0, s2 = 0;
int k, i, j;
cin >> k;
while (k-- > 0) {
cin >> i >> j;
i -= 1;
j -= 1;
if (i >= n || j >= m) {
s1 = -1;
}

if (s1 != -1) {
s1 += a[i][j];
}

if (i >= m || j >= n) {
s2 = -1;
}

if (s2 != -1) {
s2 += a[j][i];
}
}

cout << max(s1, s2) << endl;
return 0;
}``````

## Mike and Matrices CodeChef Solution in PYTH 3

``````# cook your dish here
n, m = map(int, input().split())
matrix = []
for _ in range (n):
matrix.append([int(x) for x in input().split()])

k = int(input())
pairs = []
for _ in range (k):
pairs.append([int(x) for x in input().split()])

E1, E2 = 0, 0
for i in range(k):
x, y = pairs[i][0], pairs[i][1]
if x > n or y > m:
E1 = -1
break
else :
E1 += matrix[x-1][y-1]

for i in range(k):
x, y = pairs[i][0], pairs[i][1]
if x > m or y > n:
E2 = -1
break
else :
E2 += matrix[y-1][x-1]

print(max(E1, E2))``````

## Mike and Matrices CodeChef Solution in C

``````#include <stdio.h>

int main ()
{
int N,M;

scanf( "%d%d", &N, &M );

long int A[N][M];

for ( int i = 0 ; i <= N - 1 ; i++ )

{

for ( int j = 0 ; j <= M - 1 ; j++ )

{

scanf( "%ld", &A[i][j] );

}

}

long int L;

scanf( "%ld", &L );

long long int e1 = 0 , e2 = 0;

int f1 = 0 , f2 = 0;

while(L--)

{

int i ,j;

scanf( "%d%d", &i, &j );

i--;

j--;

if ( ( i > N - 1) || (j > M - 1) )
{
e1 = -1;

f1 = 1;

}

if ( (j > N -1) || (i > M - 1) )

{
e2 = -1;

f2 = 1;

}

if ( f1 == 0 )

{

e1 += A[i][j];

}

if ( f2 == 0 )

{

e2 += A[j][i];

}
}
printf( "%lld\n", ( e1 >= e2 ? e1 : e2 ) );
} ``````

## Mike and Matrices CodeChef Solution in JAVA

``````/* package codechef; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Codechef
{
public static void main (String[] args) throws java.lang.Exception
{
BufferedWriter writer=new BufferedWriter(new OutputStreamWriter(System.out));
int n=input[0], m=input[1];
int[][] grid=new int[n][m];
for(int i=0;i<n;i++){
}
int[][] pairs=new int[l][2];
for(int i=0;i<l;i++){
}
long s1=0L, s2=0L;
for(int[] p: pairs){
int x=p[0]-1, y=p[1]-1;
if(x>=0 && x<grid.length && y>=0 && y<grid[0].length && s1>=0){
s1+=grid[x][y];
}else{
s1=-1;
}
if(s2>=0 && x>=0 && y>=0 && x<grid[0].length && y<grid.length){
s2+=grid[y][x];
}else{
s2=-1;
}
}
writer.write(Math.max(s1, s2)+"\n");
writer.flush();
}

private static int[] parseInt(String str) {
String[] parts=str.split("\\s+");
int[] ans=new int[parts.length];
for(int i=0;i<ans.length;i++){
ans[i]=Integer.parseInt(parts[i]);
}
return ans;
}

private static long[] parseLong(String str) {
String[] parts=str.split("\\s+");
long[] ans=new long[parts.length];
for(int i=0;i<ans.length;i++){
ans[i]=Long.parseLong(parts[i]);
}
return ans;
}
}``````

## Mike and Matrices CodeChef Solution in PYPY 3

``````A = []
n,m = map(int,input().split())

for i in range(n):
A_row = list(map(int,input().split()))
A.append(A_row)

e1,e2 = 0,0

l = int(input())
flag1,flag2 = True,True
for i in range(l):
a,b = map(int,input().split())
if a<=n and b<=m and flag1:
e1+=A[a-1][b-1]
else:
e1 = -1
flag1 = False

if b<=n and a<=m and flag2:
e2+=A[b-1][a-1]
else:
e2 = -1
flag2 = False

print(max(e1,e2))``````

## Mike and Matrices CodeChef Solution in PYTH

``````st = raw_input().split()
N = int(st[0])
M = int(st[1])
Z = min(N,M)
A = [[0 for x in range(M+1)] for y in range(N+1)]
for y in range(N):
st = raw_input().split()
for x in range(M):
n = int(st[x])
A[y+1][x+1] = n
# endfor x
# endfor y
L = int(raw_input())
E1 = 0
E2 = 0
for k in range(L):
st = raw_input().split()
a = int(st[0])
b = int(st[1])
if a > Z:
if a > N:
E1 = -1
else:
E2 = -1
# endif
# endif
if b > Z:
if b > N:
E2 = -1
else:
E1 = -1
# endif
# endif
if E1 > -1:
E1 += A[a][b]
# endif
if E2 > -1:
E2 += A[b][a]
# endif
# endfor k
r = max(E1,E2)
print r
``````

## Mike and Matrices CodeChef Solution in C#

``````using System;

public class Test
{
public static void Main()
{
var n = nm[0];
var m = nm[1];

var data = new long[n, m];

for (int i = 0; i < n; i++)
{

for (int j = 0; j < m; j++)
{
data[i, j] = row[j];
}
}

long e1 = 0;
long e2 = 0;

while (l-- > 0)
{

long i = pair[0];
long j = pair[1];

if (e1 != -1 && i>0 && j>0 && i < n + 1 && j < m + 1)
{
e1 += data[i-1, j-1];
}
else
{
e1 = -1;
}

if (e2 != -1 && i>0 && j> 0 && j < n +1 && i < m + 1)
{
e2 += data[j-1, i-1];
}
else
{
e2 = -1;
}

}

Console.WriteLine(e1 > e2 ? e1 : e2);
}
}``````
##### Mike and Matrices CodeChef Solution Review:

In our experience, we suggest you solve this Mike and Matrices CodeChef Solution and gain some new skills from Professionals completely free and we assure you will be worth it.

If you are stuck anywhere between any coding problem, just visit Queslers to get the Mike and Matrices CodeChef Solution.

Find on CodeChef

##### Conclusion:

I hope this Mike and Matrices CodeChef Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Coding Solutions.

This Problem is intended for audiences of all experiences who are interested in learning about Programming Language in a business context; there are no prerequisites.

Keep Learning!