Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace MatrixMaxSum
- {
- //83
- using System;
- using System.Linq;
- class Program
- {
- static void Main(string[] args)
- {
- int rows = int.Parse(Console.ReadLine());
- int[][] jaggedArr = FillArr(rows);
- int[] coordinates = ReadData();
- int maxSum = int.MinValue;
- for (int corrdinatePair = 0; corrdinatePair < coordinates.Length; corrdinatePair += 2)
- {
- int rCoordinate = coordinates[corrdinatePair];
- int cCoordinate = coordinates[corrdinatePair + 1];
- int currSum = 0;
- int rowSum = 0;
- int colSum = 0;
- if (rCoordinate > 0)
- {
- if (cCoordinate > 0)
- {
- rCoordinate--;
- cCoordinate--;
- int[] arr = jaggedArr[rCoordinate];
- for (int row = 0; row <= cCoordinate; row++)
- {
- rowSum += arr[row];
- }
- currSum += rowSum;
- for (int col = 0; col < rCoordinate; col++)
- {
- colSum += jaggedArr[col][cCoordinate];
- }
- currSum += colSum;
- }
- else if (cCoordinate <= 0) //
- {
- cCoordinate = Math.Abs(cCoordinate);
- rCoordinate--;
- cCoordinate--;
- int[] arr = jaggedArr[rCoordinate];
- for (int row = 0; row <= cCoordinate; row++)
- {
- rowSum += arr[row];
- }
- currSum += rowSum;
- for (int col = rows - 1; col > rCoordinate; col--)
- {
- colSum += jaggedArr[col][cCoordinate];
- }
- currSum += colSum;
- }
- }
- else if (rCoordinate < 0)
- {
- if (cCoordinate > 0) //
- {
- rCoordinate = Math.Abs(rCoordinate);
- rCoordinate--;
- int[] arr = jaggedArr[rCoordinate];
- for (int row = arr.Length - 1; row >= cCoordinate - 1; row--)
- {
- rowSum += arr[row];
- }
- currSum += rowSum;
- for (int col = 0; col < rCoordinate; col++)
- {
- colSum += jaggedArr[col][cCoordinate - 1];
- }
- currSum += colSum;
- }
- else if (cCoordinate < 0)
- {
- cCoordinate = Math.Abs(cCoordinate);
- rCoordinate = Math.Abs(rCoordinate);
- rCoordinate--;
- int[] arr = jaggedArr[rCoordinate];
- for (int row = arr.Length - 1; row >= cCoordinate - 1; row--)
- {
- rowSum += arr[row];
- }
- currSum += rowSum;
- for (int col = rows - 1; col > rCoordinate; col--)
- {
- colSum += jaggedArr[col][cCoordinate - 1];
- }
- currSum += colSum;
- }
- }
- if (maxSum < currSum)
- {
- maxSum = currSum;
- }
- }
- Console.WriteLine(maxSum);
- }
- public static int[][] FillArr(int r)
- {
- int[][] jaggedArr = new int[r][];
- for (int row = 0; row < r; row++)
- {
- jaggedArr[row] = ReadData();
- }
- return jaggedArr;
- }
- public static int[] ReadData() => Console.ReadLine()
- .Split()
- .Select(int.Parse)
- .ToArray();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment