Algorithms and Data Structures

  • Profound Academy

    • Status
      • 1
        Implementation
      • 2
        Bitwise operations
      • 3
        Prefix Sums
      • 4
        Sliding window / Two pointers
      • 5
        Modular Arithmetic
      • 6
        Number Theory
      • 7
        Binary Search
      • 8
        Basic Sorting
      • 9
        Greedy Algorithms
      • 10
        Basic Dynamic Programming
      • 11
        Recursion
      • 12
        Linked LIst
      • 13
        Queue & Stack
      • 14
        Binary tree + BST
      • 15
        Divide & Conquer + Advanced Sorting
      • 16
        Heap
      • 17
        Hashing
      • 18
        Graph Representation
      • 19
        BFS

  • Playing a game of removing numbers

    Let’s play a game. You are given n positive integers (n is even). On each step, you are allowed to remove a number from the beginning or the very end of the sequence and keep it to yourself. I (the computer) will remove the largest among the first and the last elements in the sequence. In case those are equal, I’ll remove the first one. We will take turns until there are no items left on the list.
    If you are the one starting the game, what would be the maximum sum of numbers you can collect?

    Input

    The first line of the input contains a single integer n (1 ≤ n ≤ 1000).
    The next line contains n space-separated integers (1 ≤ ).

    Output

    The program should print the maximum sum you can collect.

    Examples

    Input
    Output
    4 3 2 10 4
    13

    Explanation

    1. You take the first 3 → 2 10 4
    1. The computer takes the 4 → 2 10
    1. You take the 10 → 2
    1. The computer takes the 2 ⇒ You’ve collected 3 + 10 = 13
     

    Constraints

    Time limit: 1 seconds

    Memory limit: 512 MB

    Output limit: 1 MB

    To check your solution you need to sign in
    Sign in to continue