Skip to content

Commit 043849a

Browse files
Create Amusement Park.cpp
1 parent ef99d2a commit 043849a

File tree

1 file changed

+71
-0
lines changed

1 file changed

+71
-0
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
#include <iostream>
2+
#include <vector>
3+
#include <map>
4+
#include <algorithm>
5+
#include <queue>
6+
7+
using namespace std;
8+
9+
long long sum(long long left, long long right)
10+
{
11+
if(left == 0)
12+
{
13+
return (right*(right + 1))/2;
14+
}
15+
16+
return sum(0, right) - sum(0, left - 1);
17+
}
18+
19+
int main()
20+
{
21+
long long no_of_elements, k;
22+
cin >> no_of_elements >> k;
23+
24+
vector <long long> A(no_of_elements + 1);
25+
for(int i = 1; i <= no_of_elements; i++)
26+
{
27+
cin >> A[i];
28+
}
29+
30+
priority_queue <long long> Q;
31+
Q.push(0);
32+
map <int, long long> frequency;
33+
for(int i = 1; i <= no_of_elements; i++)
34+
{
35+
frequency[A[i]]++;
36+
37+
if(frequency[A[i]] == 1)
38+
{
39+
Q.push(A[i]);
40+
}
41+
}
42+
43+
long long score = 0;
44+
while(Q.top() > 0 && k > 0)
45+
{
46+
long long x = Q.top();
47+
Q.pop();
48+
49+
long long next = Q.top();
50+
51+
if(k >= frequency[x]*(x - next) )
52+
{
53+
score += frequency[x]*sum(next + 1, x);
54+
frequency[next] += frequency[x];
55+
56+
k -= frequency[x]*(x - next);
57+
}
58+
else
59+
{
60+
long long quotient = k/frequency[x], remainder = k%frequency[x];
61+
62+
score += frequency[x]*(sum(x - quotient + 1, x));
63+
score += remainder*(x - quotient);
64+
65+
k = 0;
66+
}
67+
}
68+
69+
cout << score << "\n";
70+
return 0;
71+
}

0 commit comments

Comments
 (0)