Skip to main content

Salesforce

SalesForce OA 2023 - Minimum Books


Problem Description:

You are given N boxes, each containing a different number of books. You are required to take a minimum number of books from these boxes while adhering to the following conditions:

  1. You must take either all or none of the books in a box.
  2. You cannot take books from two adjacent boxes. That means if you take books from box 1, you cannot take books from box 2, but you can take from
    box 3.

Your task is to calculate the minimum number of books you can take, ensuring that the conditions above are met.


Input Format:

  • The first line contains a single integer N (1 ≤ N ≤ 100), representing the number of boxes.
  • The second line contains N space-separated integers, representing the number of books in each box.

Output Format:

  • Print the minimum number of books you can take, adhering to the conditions provided.

Example:

Input:

5
1 3 4 2 3

Output:

5

Explanation:

The minimum number of books you can take can be achieved by taking books from boxes 1, 3, and 5, which gives:

1 (from box 1) + 4 (from box 3) = 5