#C4238. Max Treasures

    ID: 47754 Type: Default 1000ms 256MiB

Max Treasures

Max Treasures

You are given n caves and a carrying capacity C for Max. Each cave contains a certain number of treasures represented as a list of non-negative integers. Max wants to collect a contiguous sequence of treasures (i.e. treasures from consecutive caves) such that the sum of the selected treasures does not exceed the capacity C. Your task is to compute the maximum sum of treasures that can be collected under this constraint.

Formally, given an array \(a_1, a_2, \dots, a_n\), find integers \(i\) and \(j\) with \(1 \leq i \leq j \leq n\) such that:

[ \sum_{k=i}^{j} a_k \leq C ]

and the value \(\sum_{k=i}^{j} a_k\) is maximized.

inputFormat

The input is read from standard input and consists of two lines:

  1. The first line contains two integers n and C (the number of caves and the maximum carrying capacity) separated by a space.
  2. The second line contains n non-negative integers separated by spaces, representing the treasures in each cave.

You can assume that n is at least 1.

outputFormat

Output a single integer, which is the maximum sum of a contiguous subsequence of treasures that does not exceed the capacity C. The output should be written to standard output.

## sample
5 10
1 2 3 4 5
10