Codeforces 218b Airport

Source: Internet
Author: User
B. airporttime limit per test

2 seconds

Memory limit per test

256 megabytes

Input

Standard Input

Output

Standard output

Lolek and Bolek are about to travel abroad by plane. The local airport has a special "choose your plane" offer. The offer's conditions are as follows:

  • It is up to a passenger to choose a plane to fly on;
  • If the chosen plane hasX(XLatency> limit 0) empty seats
    The given moment, then the ticket for such a plane costsXZlotys (units of Polish currency ).

The only ticket office of the airport already has a queueNPassengers in front of it. lolek and Bolek have not stood in the queue yet, but they are already
Wondering what is the maximum and the minimum number of zlotys the airport administration can earn if allNPassengers buy tickets according to the conditions
Of this offer?

The passengers buy tickets in turn, the first person in the queue goes first, then goes the second one, and so on upN-Th person.

Input

The first line contains two integersNAndM(1 digit ≤ DigitN, Bytes,MLimit ≤ limit 1000 )-
The number of passengers in the queue and the number of planes in the airport, correspondingly. The next line containsMIntegersA1, bytes,A2, middle..., middle ,...,AM(1 digit ≤ DigitAILimit ≤ limit 1000 )-AIStands
For the number of empty seats inI-Th plane before the ticket office starts selling tickets.

The numbers in the lines are separated by a space. It is guaranteed that there are at leastNEmpty seats in total.

Output

Print two integers-the maximum and the minimum number of zlotys that the airport administration can earn, correspondingly.

Sample test (s) Input
4 32 1 1
Output
5 5
Input
4 32 2 2
Output
7 6
Note

In the first test sample the number of passengers is equal to the number of empty seats, so regardless of the way the planes are chosen, the Administration will earn the same sum.

In the second sample the sum is maximized if the 1-st person in the queue buys a ticket to the 1-st plane, the 2-nd person-to the 2-nd plane, the 3-rd person-to the 3-rd plane, the 4-th person-to the 1-st plane. the sum is minimized if the 1-st person
In the queue buys a ticket to the 1-st plane, the 2-nd person-to the 1-st plane, the 3-rd person-to the 2-nd plane, the 4-th person-to the 2-nd plane.

________________________________________________ Train of thought: greedy and priority queue. When the maximum value is obtained, the aircraft with the most available space is selected each time. The minimum value means that the aircraft with the least available space is selected each time. The question is very watery. However, priority_queue is currently used during the competition, so I will paste it to commemorate it.
# Include <iostream> # include <cstdio> # include <cstdlib> # include <cmath> # include <cstring> # include <string> # include <vector> # include <list> # include <deque> # include <queue> # include <iterator> # include <stack> # include <map> # include <set> # include <algorithm> # include <cctype> using namespace STD; typedef long ll; const int n = 1010; const int INF = 0x3f3f3f; int A [n]; int main () {int n, m, maxm = 0, minm = 0; CIN> N> m; memset (A, 0, sizeof (a); priority_queue <int> q1; // The default value is the top heap priority_queue <int, vector <int>, greater <int> Q2; For (INT I = 1; I <= m; I ++) {CIN> A [I]; q1.push (A [I]); q2.push (A [I]);} For (INT I = 0; I <n; I ++) {int T = q1.top (); q1.pop (); maxm + = T; t --; q1.push (t) ;}for (INT I = 0; I <n; I ++) {int T = q2.top (); q2.pop (); minm + = T; t --; If (T! = 0) q2.push (t);} printf ("% d", maxm, minm); Return 0 ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.