Advertisement
ShadowEmbrace

Jump Around

Oct 16th, 2018
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.47 KB | None | 0 0
  1. <?php
  2.  
  3. $nums = array_map('intval', explode(' ', readline()));
  4.  
  5. $step = $nums[0];
  6. $stepPrev = 0;
  7. $sum = $step;
  8.  
  9. while(true){
  10.     if (($stepPrev + $step) < count($nums)){
  11.         $sum += $nums[$stepPrev+$step];
  12.         $stepPrev += $step;
  13.     }elseif (($stepPrev + $step) > count($nums) && ($stepPrev - $step) >= 0){
  14.         $sum += $nums[$stepPrev-$step];
  15.         $stepPrev -= $step;
  16.     }else{
  17.         echo $sum;
  18.         return;
  19.     }
  20.     $step = $nums[$stepPrev];
  21. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement