CF1175D.Array Splitting

普及+/提高

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

You are given an array a1,a2,…,ana_1, a_2, \dots, a_n and an integer kk.

You are asked to divide this array into kk non-empty consecutive subarrays. Every element in the array should be included in exactly one subarray. Let f(i)f(i) be the index of subarray the ii-th element belongs to. Subarrays are numbered from left to right and from 11 to kk.

Let the cost of division be equal to ∑i=1n(ai⋅f(i))\sum\limits_{i=1}^{n} (a_i \cdot f(i)). For example, if a=[1,−2,−3,4,−5,6,−7]a = [1, -2, -3, 4, -5, 6, -7] and we divide it into 33 subbarays in the following way: [1,−2,−3],[4,−5],[6,−7][1, -2, -3], [4, -5], [6, -7], then the cost of division is equal to 1⋅1−2⋅1−3⋅1+4⋅2−5⋅2+6⋅3−7⋅3=−91 \cdot 1 - 2 \cdot 1 - 3 \cdot 1 + 4 \cdot 2 - 5 \cdot 2 + 6 \cdot 3 - 7 \cdot 3 = -9.

Calculate the maximum cost you can obtain by dividing the array aa into kk non-empty consecutive subarrays.

给你一个数组 a1,a2,…,ana_1, a_2, \dots, a_n 和一个整数 kk。

你需要将该数组划分为 kk 个非空且连续的子数组。数组中的每个元素必须且仅被包含在其中一个子数组中。令 f(i)f(i) 表示第 ii 个元素所属子数组的编号。子数组从左到右依次编号,编号范围为 11 到 kk。

定义划分的代价为 ∑i=1n(ai⋅f(i))\sum\limits_{i=1}^{n} (a_i \cdot f(i))。例如,若 a=[1,−2,−3,4,−5,6,−7]a = [1, -2, -3, 4, -5, 6, -7],并将它按如下方式划分为 33 个子数组:[1,−2,−3],[4,−5],[6,−7][1, -2, -3], [4, -5], [6, -7],则划分的代价为
1⋅1−2⋅1−3⋅1+4⋅2−5⋅2+6⋅3−7⋅3=−91 \cdot 1 - 2 \cdot 1 - 3 \cdot 1 + 4 \cdot 2 - 5 \cdot 2 + 6 \cdot 3 - 7 \cdot 3 = -9。

请计算将数组 aa 划分为 kk 个非空连续子数组所能得到的最大代价。

输入格式

The first line contains two integers nn and kk (1≤k≤n≤3⋅1051 \le k \le n \le 3 \cdot 10^5).

The second line contains nn integers a1,a2,…,ana_1, a_2, \dots, a_n ($ |a_i| \le 10^6$).

第一行包含两个整数 nn 和 kk(1≤k≤n≤3⋅1051 \le k \le n \le 3 \cdot 10^5)。

第二行包含 nn 个整数 a1,a2,…,ana_1, a_2, \dots, a_n(∣ai∣≤106|a_i| \le 10^6)。

输出格式

Print the maximum cost you can obtain by dividing the array aa into kk nonempty consecutive subarrays.

输出将数组 aa 划分为 kk 个非空连续子数组所能获得的最大代价。

输入输出样例

  • 输入#1

    5 2
    -1 -2 5 -4 8

    输出#1

    15
  • 输入#2

    7 6
    -3 0 -1 -2 -2 -4 -1

    输出#2

    -45
  • 输入#3

    4 1
    3 -1 6 0

    输出#3

    8

输入解题思路,AI测评打分。不知道怎么写?

首页