Sashka has a sequence of positive integers, a positive integer , and an arithmetic progression, defined by its first element and the difference between its consecutive elements , which are both positive integers. She is interested in the subsequences of length of denoted as . Write a program arithmetic_progression that calculates the minimal sum of respective multiplications of such a subsequence with the first entries of the arithmetic progression, meaning the following sum is minimized:
A sequence is an arithmetic progression if and only if for all for some constant .
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)
- : vector, containing
- : the length of the subsequence
- : the first entry of the arithmetic progression
- : 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
| Subtask | Points | Required subtasks | Other constraints | |
|---|---|---|---|---|
| 0 | 0 | - | - | The sample tests. |
| 1 | 5 | 0 | - | |
| 2 | 6 | 0-1 | - | |
| 3 | 3 | 0-2 | - | |
| 4 | 1 | - | ||
| 5 | 4 | 4 | ||
| 6 | 4 | 4-5 | ||
| 7 | 5 | - | is obtained by uniformly shuffling a sequence that is chosen by the author for the given test. | |
| 8 | 5 | 0-7 | - | |
| 9 | 67 | 0-8 | - |
Local testing
Input format:
- line 1: ;
- line 2: .
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 . We choose , and we get the optimal result:
Example 2
stdin
3 2 6 1
5 1 4
stdout
34
Explanation
The arithmetic progression is . We choose , and we get the optimal result:
Example 3
stdin
6 4 4 6
18 12 8 14 19 11
stdout
562
Explanation
The arithmetic progression is . We choose , and we get the optimal result: