luogu#P16374. [IATI 2026] Arithmetic Progression

    ID: 16553 远端评测题 5000ms 1024MiB 尝试: 0 已通过: 0 难度: 9 上传者: 标签>动态规划 DP树形数据结构二分平衡树交互题动态规划优化2026斜率维护技巧 slope trickIATI(保加利亚/东欧)

[IATI 2026] Arithmetic Progression

Problem Description

Sashka has a sequence A0,A1,,AN1A_0,A_1,\dots,A_{N-1} of NN positive integers, a positive integer KK, and an arithmetic progression (A sequence P0,P1,P_0, P_1,\dots is an arithmetic progression if and only if Pi=D+Pi1P_i = D + P_{i-1} for all i1i \geq 1 for some constant DD.), defined by its first element SS and the difference between its consecutive elements DD, which are both positive integers.

She is interested in the subsequences (A subsequence of a sequence is obtained by removing zero or more elements from it, without changing the order of the remaining elements.) of length KK of AA denoted as B0,B1,,BK1B_0, B_1,\dots,B_{K-1}.

Write a program arithmetic_progression\texttt{arithmetic\_progression} that calculates the minimal sum of respective multiplications of such a subsequence with the first KK entries of the arithmetic progression, meaning the following sum is minimized:

i=0K1Bi×(S+i×D)\sum_{i=0}^{K-1} B_i \times (S + i \times D)

Implementation details

You should implement the function solve\verb|solve|:

 __int128 solve(std::vector <long long> A, int K, long long S, int D)
  • AA: vector, containing A0,A1,,AN1A_0,A_1, \dots, A_{N-1}
  • KK: the length of the subsequence
  • SS: the first entry of the arithmetic progression
  • DD: the difference of the arithmetic progression

Your function should return the minimal possible sum of the described type. As it may be too large, your function should return the variable of the non-standard __int128\texttt{\_\_int128} type for 128128 bit integers. The header file arithmetic_progression.h\texttt{arithmetic\_progression.h} contains overloaded definitions for the <<\texttt{<<} and >>\texttt{>>} operators, allowing the use of std::cin\verb|std::cin|, std::cout\verb|std::cout| and std::cerr\verb|std::cerr| for __int128\verb|__int128| variables.

Input Format

Input format:

  • line 1: NN KK SS DD;
  • line 2: A0 A1 A2 ... AN1A_0\ A_1\ A_2\ ...\ A_{N-1}.

Output Format

Output format:

  • line 1: one integer, equal to the answer returned by \verb|solve|
3 2 1 1
5 1 4
7
3 2 6 1
5 1 4
34
6 4 4 6
18 12 8 14 19 11
562

Hint

Comment 1

The arithmetic progression is {1,2}\{1,2\}. We choose B={5,1}B=\{5,1\}, and we get the optimal result: 5×1+1×2=75 \times 1 + 1 \times 2 = 7

Comment 2

The arithmetic progression is {6,7}\{6,7\}. We choose B={1,4}B=\{1,4\}, and we get the optimal result: 1×6+4×7=341 \times 6 + 4 \times 7 = 34

Comment 3

The arithmetic progression is {4,10,16,22}\{4,10,16,22\}. We choose B={18,12,8,11}B=\{18,12,8,11\}, and we get the optimal result: $18 \times 4 + 12 \times 10 + 8 \times 16 + 11 \times 22 = 562$

Constraints

  • 1KN300 0001 \leq K \leq N \leq 300\ 000
  • 1D1091 \leq D \leq 10^9
  • 1Ai,S10151 \leq A_i, S \leq 10^{15}

Subtasks

Subtask Points Required subtasks NN Other constraints
00 - The sample tests.
11 55 00 20\leq 20 -
22 66 010-1 500\leq 500
33 020-2 3 000\leq 3\ 000
44 11 - 100 000\leq 100\ 000 K=NK = N
55 44 44 KN1K \geq N - 1
66 454-5 KN2K \geq N-2
77 55 - AA is obtained by uniformly shuffling a sequence TT that is chosen by the author for the given test.
88 070-7 -
99 6767 080-8 300 000\leq 300\ 000

The points for a given subtask are obtained only if all the tests for it and the required subtasks are successfully passed.