Advertisement
Guest User

Untitled

a guest
Jun 13th, 2017
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.99 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3.  
  4. namespace _09.Jump_Around
  5. {
  6.     class Program
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             long[] numbers = Console.ReadLine()
  11.                 .Split(' ')
  12.                 .Select(x => long.Parse(x))
  13.                 .ToArray();
  14.  
  15.             long maxIndex = numbers.Length - 1;
  16.  
  17.             long sum = 0;
  18.             long index = 0;
  19.            
  20.             while (true)
  21.             {
  22.                 sum += numbers[index];
  23.  
  24.                 long nextIndex = index + numbers[index];
  25.  
  26.                 if (nextIndex <= maxIndex)
  27.                 {
  28.                     index = nextIndex;
  29.                     continue;
  30.                 }
  31.  
  32.                 nextIndex = index - numbers[index];
  33.                 if (nextIndex >= 0)
  34.                 {
  35.                     index = nextIndex;
  36.                     continue;
  37.                 }
  38.  
  39.                 break;
  40.             }
  41.  
  42.             Console.WriteLine(sum);
  43.         }
  44.     }
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement