Given an integer **N** and an array **seats[]** where **N** is the number of people standing in a line to buy a movie ticket and **seat[i]** is the number of empty seats in the **i ^{th}** row of the movie theater. The task is to find the maximum amount a theater owner can make by selling movie tickets to

**N**people. Price of a ticket is equal to the maximum number of empty seats among all the rows.

**Example:**

Input:seats[] = {1, 2, 4}, N = 3

Output:9

Person Empty Seats Ticket Cost 1 1 2 4 4 2 1 2 3 3 3 1 2 2 2 4 + 3 + 2 = 9

Input:seats[] = {2, 3, 5, 3}, N = 4

Output:15

**Approach:** This problem can be solved by using a priority queue that will store the count of empty seats for every row and the maximum among them will be available at the top.

- Create an empty priority_queue
**q**and traverse the**seats[]**array and insert all element into the priority_queue. - Initialize two integer variable
**ticketSold = 0**and**ans = 0**that will store the number of tickets sold and the total collection of the amount so far. - Now check while
**ticketSold < N**and**q.top() > 0**then remove the top element from the priority_queue and update ans by adding top element of the priority queue. Also store this top value in a variable**temp**and insert**temp – 1**back to the priority_queue. - Repeat these steps until all the people have been sold the tickets and print the final result.

Below is the implementation of the above approach:

## C++

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to return the maximum amount ` `// that can be collected by selling tickets ` `int` `maxAmount(` `int` `M, ` `int` `N, ` `int` `seats[]) ` `{ ` ` ` ` ` `// Priority queue that stores ` ` ` `// the count of empty seats ` ` ` `priority_queue<` `int` `> q; ` ` ` ` ` `// Insert each array element ` ` ` `// into the priority queue ` ` ` `for` `(` `int` `i = 0; i < M; i++) { ` ` ` `q.push(seats[i]); ` ` ` `} ` ` ` ` ` `// To store the total ` ` ` `// number of tickets sold ` ` ` `int` `ticketSold = 0; ` ` ` ` ` `// To store the total amount ` ` ` `// of collection ` ` ` `int` `ans = 0; ` ` ` ` ` `// While tickets sold are less than N ` ` ` `// and q.top > 0 then update the collected ` ` ` `// amount with the top of the priority ` ` ` `// queue ` ` ` `while` `(ticketSold < N && q.top() > 0) { ` ` ` `ans = ans + q.top(); ` ` ` `int` `temp = q.top(); ` ` ` `q.pop(); ` ` ` `q.push(temp - 1); ` ` ` `ticketSold++; ` ` ` `} ` ` ` ` ` `return` `ans; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `seats[] = { 1, 2, 4 }; ` ` ` `int` `M = ` `sizeof` `(seats) / ` `sizeof` `(` `int` `); ` ` ` `int` `N = 3; ` ` ` ` ` `cout << maxAmount(N, M, seats); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the approach ` `import` `java.util.*; ` ` ` `class` `GFG ` `{ ` ` ` `static` `int` `[] seats = ` `new` `int` `[]{ ` `1` `, ` `2` `, ` `4` `}; ` ` ` ` ` `// Function to return the maximum amount ` ` ` `// that can be collected by selling tickets ` ` ` `public` `static` `int` `maxAmount(` `int` `M, ` `int` `N) ` ` ` `{ ` ` ` ` ` `// Priority queue that stores ` ` ` `// the count of empty seats ` ` ` `PriorityQueue<Integer> q = ` ` ` `new` `PriorityQueue<Integer>(Collections.reverseOrder()); ` ` ` ` ` `// Insert each array element ` ` ` `// into the priority queue ` ` ` `for` `(` `int` `i = ` `0` `; i < M; i++) ` ` ` `{ ` ` ` `q.add(seats[i]); ` ` ` `} ` ` ` ` ` `// To store the total ` ` ` `// number of tickets sold ` ` ` `int` `ticketSold = ` `0` `; ` ` ` ` ` `// To store the total amount ` ` ` `// of collection ` ` ` `int` `ans = ` `0` `; ` ` ` ` ` `// While tickets sold are less than N ` ` ` `// and q.top > 0 then update the collected ` ` ` `// amount with the top of the priority ` ` ` `// queue ` ` ` `while` `(ticketSold < N && q.peek() > ` `0` `) ` ` ` `{ ` ` ` `ans = ans + q.peek(); ` ` ` `int` `temp = q.peek(); ` ` ` `q.poll(); ` ` ` `q.add(temp - ` `1` `); ` ` ` `ticketSold++; ` ` ` `} ` ` ` `return` `ans; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `main(String[] args) ` ` ` `{ ` ` ` `int` `M = seats.length; ` ` ` `int` `N = ` `3` `; ` ` ` ` ` `System.out.print(maxAmount(M, N)); ` ` ` `} ` `} ` ` ` `// This code is contributed by Sanjit_Prasad ` |

*chevron_right*

*filter_none*

## Python 3

`# Python 3 implementation of the approach ` ` ` `# Function to return the maximum amount ` `# that can be collected by selling tickets ` `def` `maxAmount(M, N, seats): ` ` ` ` ` `# Priority queue that stores ` ` ` `# the count of empty seats ` ` ` `q ` `=` `[] ` ` ` ` ` `# Insert each array element ` ` ` `# into the priority queue ` ` ` `for` `i ` `in` `range` `(M): ` ` ` `q.append(seats[i]) ` ` ` ` ` `# To store the total ` ` ` `# number of tickets sold ` ` ` `ticketSold ` `=` `0` ` ` ` ` `# To store the total amount ` ` ` `# of collection ` ` ` `ans ` `=` `0` ` ` ` ` `# While tickets sold are less than N ` ` ` `# and q.top > 0 then update the collected ` ` ` `# amount with the top of the priority ` ` ` `# queue ` ` ` `q.sort(reverse ` `=` `True` `) ` ` ` `while` `(ticketSold < N ` `and` `q[` `0` `] > ` `0` `): ` ` ` `ans ` `=` `ans ` `+` `q[` `0` `] ` ` ` `temp ` `=` `q[` `0` `] ` ` ` `q ` `=` `q[` `1` `:] ` ` ` `q.append(temp ` `-` `1` `) ` ` ` `q.sort(reverse ` `=` `True` `) ` ` ` `ticketSold ` `+` `=` `1` ` ` ` ` `return` `ans ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `'__main__'` `: ` ` ` `seats ` `=` `[` `1` `, ` `2` `, ` `4` `] ` ` ` `M ` `=` `len` `(seats) ` ` ` `N ` `=` `3` ` ` ` ` `print` `(maxAmount(N, M, seats)) ` ` ` `# This code is contributed by Surendra_Gangwar ` |

*chevron_right*

*filter_none*

**Output:**

9

## Recommended Posts:

- Maximize the profit after selling the tickets
- Maximum profit by selling N items at two markets
- Maximum profit after buying and selling the stocks
- Maximum profit by buying and selling a stock at most twice | Set 2
- Maximum profit by buying and selling a share at most K times | Greedy Approach
- Calculate the total fine to be collected
- Find all unique pairs of maximum and second maximum elements over all sub-arrays in O(NlogN)
- Maximise the number of toys that can be purchased with amount K
- Maximize the profit by selling at-most M products
- Maximise the number of toys that can be purchased with amount K using min Heap
- Maximize profit in buying and selling stocks with Rest condition
- Calculate the loss incurred in selling the given items at discounted price
- Find the maximum subset XOR of a given set
- Find the row with maximum number of 1s
- Given an array arr[], find the maximum j - i such that arr[j] > arr[i]
- Find the maximum value of Y for a given X from given set of lines
- Find the Maximum possible Sum for the given conditions
- Find Maximum value of abs(i - j) * min(arr[i], arr[j]) in an array arr[]
- Find the maximum possible value of a[i] % a[j] over all pairs of i and j
- Find two numbers with given sum and maximum possible LCM

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.