luogu#P10977. Cut the Sequence

    ID: 10749 远端评测题 1000ms 512MiB 尝试: 0 已通过: 0 难度: 6 上传者: 标签>动态规划 DP线段树单调队列动态规划优化线性 DPSTL

Cut the Sequence

Problem Description

Given an integer sequence {aN}\{a_N\} of length NN, you need to split this sequence into several parts such that each part is a contiguous subsequence of the original sequence, and the sum of integers in each part does not exceed MM. Your task is to find, among all valid splitting ways, the minimum possible value of the sum of the maximum value of each part.

Input Format

The first line contains two integers NN and MM (1N1051 \le N \le 10^5, 1M<2311 \le M < 2^{31}). The second line contains NN integers a1,a2,,aNa_1,a_2,\dots,a_N (0ai1060 \le a_i \le 10^6).

Output Format

Output one integer, which is the minimum possible value of the sum of the maximum value of each part. If there is no valid splitting way, output 1-1.

8 17
2 2 2 8 1 8 2 1
12

Hint

Sample explanation:

Split the sequence into three parts: {2,2,2},{8,1,8},{2,1}\{2,2,2\},\{8,1,8\},\{2,1\}. Then the sum of the maximum value of each part is 2+8+2=122+8+2=12. It can be proven that there is no better solution.

Translated by ChatGPT 5