Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- namespace _9._MatrixMaxSum
- {
- class Program
- {
- static void Main(string[] args)
- {
- int n = int.Parse(Console.ReadLine());
- string[] input = Console.ReadLine().Split(' ');
- int[,] matrix = new int[n, input.Length];
- int sum = 0;
- int maxSum = int.MinValue;
- for (int row = 0; row < n; row++)
- {
- for (int col = 0; col < input.Length; col++)
- {
- matrix[row, col] = int.Parse(input[col]);
- }
- if (row != n - 1)
- {
- input = Console.ReadLine().Split(' ');
- }
- }
- int[] coordinates = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
- for (int i = 0; i < coordinates.Length; i+=2)
- {
- if (coordinates[i] > 0)
- {
- for (int col = 0; col < Math.Abs(coordinates[i + 1]); col++)
- {
- sum += matrix[coordinates[i] - 1, col];
- }
- }
- else
- {
- for (int col = input.Length - 1; col >= Math.Abs(coordinates[i + 1]) - 1; col--)
- {
- sum += matrix[Math.Abs(coordinates[i]) - 1, col];
- }
- }
- if (coordinates[i + 1] > 0)
- {
- for (int row = Math.Abs(coordinates[i]) - 2; row >= 0; row--)
- {
- sum += matrix[row, coordinates[i + 1] - 1];
- }
- }
- else
- {
- for (int row = Math.Abs(coordinates[i]); row < n; row++)
- {
- sum += matrix[row, Math.Abs(coordinates[i + 1]) - 1];
- }
- }
- if (sum > maxSum)
- {
- maxSum = sum;
- }
- sum = 0;
- }
- Console.WriteLine(maxSum);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment