Advertisement
valchak

Jump_Around

Jun 10th, 2017
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.24 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3.  
  4. public class Jump_Around
  5. {
  6.     public static void Main()
  7.     {
  8.         var inputArr = Console.ReadLine()
  9.             .Trim()
  10.             .Split(' ')
  11.             .Select(int.Parse)
  12.             .ToArray();
  13.  
  14.         var indexRight = inputArr[0];
  15.         var sum = inputArr[0];
  16.         var indexLeft = -1;
  17.  
  18.         try
  19.         {
  20.             while (true)
  21.             {
  22.                 if (indexRight < inputArr.Length && indexRight >= 0)
  23.                 {
  24.                     sum += inputArr[indexRight];
  25.                     indexLeft = indexRight - inputArr[indexRight];
  26.                     indexRight += inputArr[indexRight];
  27.                 }
  28.                 else if (indexLeft < inputArr.Length && indexLeft >= 0)
  29.                 {
  30.                     sum += inputArr[indexLeft];
  31.                     indexLeft -= inputArr[indexLeft];
  32.                     indexRight = indexLeft + inputArr[indexLeft];
  33.                 }
  34.                 else
  35.                 {
  36.                     Console.WriteLine(sum);
  37.                     return;
  38.                 }
  39.             }
  40.         }
  41.  
  42.         catch (IndexOutOfRangeException)
  43.         {
  44.             Console.WriteLine(sum);          
  45.         }
  46.     }
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement