Monk's favourite game is Football and his favourite club is "Manchester United". Manchester United has qualified for the Champions League Final which is to be held at the Wembley Stadium in London. So, he decided to go there and watch his favourite team play. After reaching the stadium, he saw that many people have lined up for the match tickets. He knows that there are M rows in the stadium with different seating capacities. They may or may not be equal. The price of the ticket depends on the row. If the row has K(always greater than 0) vacant seats, then the price of the ticket will be K pounds(units of British Currency). Now, every football fan standing in the line will get a ticket one by one.
Given the seating capacities of different rows, find the maximum possible pounds that the club will gain with the help of the ticket sales.
Example:
Input: m = 3, n = 4, a = [1,2,4]
Output: 11
Approach
C++
#include <bits/stdc++.h>using namespace std;int championLegue(int m, int n, int a[]){priority_queue<int> q;for (int i = 0; i < m; i++){q.push(a[i]);}int ans = 0;while (!q.empty() && n > 0){ans += q.top();int x = q.top();q.pop();if (x > 1)q.push(x - 1);n--;}return ans;}int main(){int m = 3, n = 4;int a[m] = {1, 2, 4};cout << championLegue(m, n, a) << "\n";return 0;}
No comments:
Post a Comment