#P15347. [TOIP 2025] 疊加最大值

[TOIP 2025] 疊加最大值

Problem Description

In a positive integer array or subarray, for each distinct number, multiply the number by its number of occurrences; this is called its superposition value. A “subarray” means a part of an array made up of consecutive elements. For example, in the array [4,5,2,3,4,2,2][4, 5, 2, 3, 4, 2, 2], the superposition value of 55 is 5×1=55\times1=5, the superposition value of 44 is 4×2=84\times2=8, the superposition value of 33 is 3×1=33\times1=3, and the superposition value of 22 is 2×3=62\times3=6.

Given a positive integer array of length nn and a positive integer kk, this problem asks for the maximum superposition value in every subarray of length kk. There are n−k+1n-k+1 subarrays of length kk in total. Output the sum of these n−k+1n-k+1 maximum superposition values.

For example, the input array is [4,5,2,3,4,2,2,5][4, 5, 2, 3, 4, 2, 2, 5], with n=8n=8 and k=5k=5. The maximum superposition values of each subarray are:

  • For [4,5,2,3,4][4, 5, 2, 3, 4], the maximum superposition value is 4×2=84\times2=8.
  • For [5,2,3,4,2][5, 2, 3, 4, 2], the maximum superposition value is 5×1=55\times1=5.
  • For [2,3,4,2,2][2, 3, 4, 2, 2], the maximum superposition value is 2×3=62\times3=6.
  • For [3,4,2,2,5][3, 4, 2, 2, 5], the maximum superposition value is 5×1=55\times1=5.

The sum of all maximum superposition values is 8+5+6+5=248+5+6+5=24.

Input Format

$$\begin{aligned} &n \; k \\ &c_0 \; c_1 \; \cdots \; c_{n-1} \end{aligned}$$
  • nn is the length of the array.
  • kk is the required subarray length.
  • cic_i is the ii-th positive integer in the array.

Output Format

XX

  • XX is the sum of all maximum superposition values.
8 5
4 5 2 3 4 2 2 5
24

Hint

Constraints

  • 1≤n≤1051 \le n\le 10^5.
  • 1≤k≤n1 \le k \le n.
  • 1≤ci<2311 \le c_i < 2^{31}.

Scoring

This problem has three subtasks with the following constraints.
Each subtask may contain one or more testdata files. You will get the score for a subtask only if you answer all testdata in that subtask correctly.

Subtask Score Additional Input Constraints
1 6 The input satisfies N≤1000N \le 1000, and ci≤106c_i \le 10^6.
2 38 The input satisfies that all numbers in the array are pairwise distinct.
3 56 No additional constraints.

Translated by ChatGPT 5