Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- class Program
- {
- static void Main()
- {
- string[] strData = Console.ReadLine().Split(new string[]{","},StringSplitOptions.RemoveEmptyEntries);
- int[] data = new int[strData.Length];
- for (int i = 0; i < strData.Length; i++)
- {
- data[i] = int.Parse(strData[i]);
- }
- int start = int.Parse(Console.ReadLine());
- int max = int.Parse(Console.ReadLine());
- int[,] matrix = new int[data.Length+1, max + 1];
- matrix[0, start] = 1;
- for (int x = 0; x < matrix.GetLength(0)-1; x++)
- {
- for (int y = 0; y < matrix.GetLength(1); y++)
- {
- if (matrix[x,y] == 1)
- {
- if (y + data[x] < matrix.GetLength(1))
- {
- matrix[x + 1, y + data[x]] = 1;
- }
- if (y - data[x] >= 0)
- {
- matrix[x + 1, y - data[x]] = 1;
- }
- }
- }
- }
- int result = 0;
- for (int y = matrix.GetLength(1)-1; y >= 0 ; y--)
- {
- if (matrix[data.Length,y] == 1)
- {
- result = y;
- break;
- }
- }
- if (result > 0)
- {
- Console.WriteLine(result);
- }
- else if (data.Length == 0)
- {
- Console.WriteLine(start);
- }
- else
- {
- Console.WriteLine(-1);
- }
- }
- private static void PrintMatrix(int[,] matrix)
- {
- for (int x = 0; x < matrix.GetLength(0); x++)
- {
- for (int y = 0; y < matrix.GetLength(1); y++)
- {
- Console.Write(matrix[x,y]);
- }
- Console.WriteLine();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement