C. Ticket Hoarding
time limit per test: 2 seconds
memory limit per test: 256 megabytes
input: standard input
output: standard output
Maître Gims - Est-ce que tu m’aimes ?
ඞ
As the CEO of a startup company, you want to reward each of your k k k employees with a ticket to the upcoming concert. The tickets will be on sale for n n n days, and by some time travelling, you have predicted that the price per ticket at day i i i will be a i a_i ai. However, to prevent ticket hoarding, the concert organizers have implemented the following measures:
- A person may purchase no more than m m m tickets per day.
- If a person purchases x x x tickets on day i i i, all subsequent days (i.e. from day i + 1 i+1 i+1 onwards) will have their prices per ticket increased by x x x.
For example, if a = [ 1 , 3 , 8 , 4 , 5 ] a = [1, 3, 8, 4, 5] a=[1,3,8,4,5] and you purchase 2 2 2 tickets on day 1 1 1, they will cost 2 2 2 in total, and the prices from day 2 2 2 onwards will become [ 5 , 10 , 6 , 7 ] [5, 10, 6, 7] [5,10,6,7]. If you then purchase 3 3 3 more tickets on day 2 2 2, they will cost in total an additional 15 15 15, and the prices from day 3 3 3 onwards will become [ 13 , 9 , 10 ] [13, 9, 10] [13,9,10].
Find the minimum spending to purchase k k k tickets.
Input
Each test contains multiple test cases. The first line contains an integer t t t ( 1 ≤ t ≤ 1 0 4 1 \le t \le 10^4 1≤t≤104) — the number of test cases. The description of the test cases follows.
The first line of each test case contains three integers n n n, m m m, and k k k ( 1 ≤ n ≤ 3 ⋅ 1 0 5 , 1 ≤ m ≤ 1 0 9 , 1 ≤ k ≤ min ( n m , 1 0 9 ) 1 \le n \le 3 \cdot 10^5, 1 \le m \le 10^9, 1 \le k \le \min(nm, 10^9) 1≤n≤3⋅105,1≤m≤109,1≤k≤min(nm,109)) — the number of sale days, the maximum amount of ticket purchasable each day, and the number of tickets to be bought at the end.
The second line of each test case contains n n n integers a 1 , a 2 , … , a n a_1, a_2, \ldots, a_n a1,a2,…,an ( 1 ≤ a i ≤ 1 0 9 1 \le a_i \le 10^9 1≤ai≤109) — the price per ticket for each of the upcoming n n n days.
It is guaranteed that the sum of n n n over all test cases does not exceed 3 ⋅ 1 0 5 3 \cdot 10^5 3⋅105.
Output
For each test case, print one integer: the minimum amount of money needed to purchase exactly
k
k
k tickets.
Example
inputCopy
4
4 2 3
8 6 4 2
4 2 8
8 6 4 2
5 100 1
10000 1 100 10 1000
6 3 9
5 5 5 5 5 5
outputCopy
10
64
1
72
AC代码:
#include<map>
#include<set>
#include<stack>
#include<cmath>
#include<queue>
#include<string>
#include<bitset>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<numeric>
#include<iomanip>
#define endl '\n'
using namespace std;
typedef long long ll;
typedef pair<int, int>PII;
const int N=3e5+10;
const int MOD=1e9 + 7;
const int INF=0X3F3F3F3F;
const int dx[]={-1,1,0,0,-1,-1,+1,+1};
const int dy[]={0,0,-1,1,-1,+1,-1,+1};
const int M = 1e6 + 10;
int t;
ll n, m, k;
ll a[N];
int main()
{
cin >> t;
while(t --){
cin >> n >> m >> k;
for(int i = 1; i <= n; i ++)
{
cin >> a[i];
}
ll sum = 0, res = 0;
sort(a + 1, a + 1 + n);
for(int i = 1; i <= n; i ++)
{
if(k >= m)
{
sum += m * a[i];
res += m;
a[i + 1] += res;
k -= m;
}
else {
sum += k * a[i];
break;
}
}
cout << sum << endl;
}
return 0;
}