#K72927. Maximum Collectable Items

    ID: 33862 Type: Default 1000ms 256MiB

Maximum Collectable Items

Maximum Collectable Items

You are given a warehouse with N slots arranged in a line. Each slot contains a certain number of items. You are also given a starting slot number k (1-indexed). You can collect items by starting at slot k and moving in one direction only, either forward (towards slot N) or backward (towards slot 1). Your task is to determine the maximum number of items you can collect by moving in one optimal direction.

In mathematical terms, let the warehouse slots be represented as an array a of length N. If you choose to move forward, you collect the sum

[ S_{forward} = \sum_{i=k}^{N} a_i, ]

and if you choose to move backward, you collect

[ S_{backward} = \sum_{i=1}^{k} a_i. ]

You need to output the maximum of these two sums.

Note: The input is provided via stdin and the answer should be printed to stdout.

inputFormat

The input consists of:

  1. A line containing two integers N and k, where N represents the number of slots (1 ≤ N ≤ 105) and k is the starting slot (1 ≤ k ≤ N).
  2. A line containing N space-separated integers representing the number of items in each slot.

outputFormat

Output a single integer: the maximum number of items that can be collected by moving either forward or backward from the starting slot.

## sample
6 4
2 1 5 3 4 6
13