arithmetic_progression

Time limit: 4.25s Memory limit: 1024MB Input: Output:

Sashka has a sequence A0,A1,...,AN1A_0, A_1, ..., A_{N-1} of NN positive integers, a positive integer KK, and an arithmetic progression, 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 of length KK of AA denoted as B0,B1,...,BK1B_0, B_1, ..., B_{K-1}. Write a program 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+iD)\sum_{i=0} ^{K-1} B_i \cdot (S + i \cdot D)

A sequence P0,P1,...P_0, P_1, ... 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.

A subsequence of a sequence is obtained by removing zero or more elements from it, without changing the order of the remaining elements.

Implementation details

You should implement the function solve:

__int128 solve(std::vector <long long> A, int K, long long S, int D)
  • AA: vector, containing A0,A1,...,AN1A_0, A_1, ..., 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 type for 128 bit integers. The header file arithmetic_progression.h contains overloaded definitions for the << and >> operators, allowing the use of std::cin, std::cout, and std::cerr, for __int128 variables.

Constraints and clarifications

  • 1K,N300 0001 \leq K, N \leq 300 \ 000
  • 1D1091 \leq D \leq 10^9
  • 1Ai,S10151 \leq A_i, S \leq 10^{15}
Subtask Points Required subtasks NN Other constraints
0 0 - - The sample tests.
1 5 0 20\leq 20 -
2 6 0-1 500\leq 500 -
3 3 0-2 3 000\leq 3 \ 000 -
4 1 - 100 000\leq 100 \ 000 K=NK = N
5 4 4 100 000\leq 100 \ 000 KN1K \geq N-1
6 4 4-5 100 000\leq 100 \ 000 KN2K \geq N-2
7 5 - 100 000\leq 100 \ 000 AA is obtained by uniformly shuffling a sequence TT that is chosen by the author for the given test.
8 5 0-7 100 000\leq 100 \ 000 -
9 67 0-8 300 000\leq 300 \ 000 -

Local testing

Input format:

  • line 1: N K S DN \ K \ S \ D;
  • line 2: A0 A1 A2...AN1A_0 \ A_1 \ A_2 ... A_{N-1}.

Output format:

  • line 1: one integer, equal to the answer returned by solve

Example 1

stdin

3 2 1 1
5 1 4

stdout

7

Explanation

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

Example 2

stdin

3 2 6 1
5 1 4

stdout

34

Explanation

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

Example 3

stdin

6 4 4 6
18 12 8 14 19 11

stdout

562

Explanation

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: 184+1210+816+2211=56218 \cdot 4 + 12 \cdot 10 + 8 \cdot 16 + 22 \cdot 11 = 562

Log in or sign up to be able to send submissions!