import java.util.*; public class Exercises19{ public static void main (String[] args){ Scanner sc = new Scanner(System.in); int numberOfSushi = sc.nextInt(); int[] sushiArray = new int[numberOfSushi]; ArrayDeque stack = new ArrayDeque(); int sum = 0; for (int n = 0; n < numberOfSushi; n++){ sushiArray[n] = sc.nextInt(); stack.add(new Sushi(sushiArray[n], n)); } while (stack.size() > 0){ Sushi sushi = stack.pop(); if(sushi.indexOfSushi + 1 >= numberOfSushi - 1){ if (sum < sushi.sumOfTaste) { sum = sushi.sumOfTaste; } }else{ int maxTaste = 0; for (int n = (sushi.indexOfSushi + 2); n < numberOfSushi; n++){ if (maxTaste < sushiArray[n]){ maxTaste = sushiArray[n]; stack.push(new Sushi(sushi.sumOfTaste + sushiArray[n], n)); } } } } System.out.println(sum); } } class Sushi { public int sumOfTaste; public int indexOfSushi; Sushi(int sum, int index){ sumOfTaste = sum; indexOfSushi = index; } }