B. Airport
time 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 has x (x > 0) empty seats at the given moment, then the ticket for such a plane costs x zlotys (units of Polish currency).
The only ticket office of the airport already has a queue of n passengers 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 all npassengers 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 up to n-th person.
Input
The first line contains two integers n and m (1 ≤ n, m ≤ 1000) — the number of passengers in the queue and the number of planes in the airport, correspondingly. The next line contains m integers a1, a2, ..., am (1 ≤ ai ≤ 1000) — ai stands for the number of empty seats in the i-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 least n empty 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 3
2 1 1
output
5 5
input
4 3
2 2 2
output
7 6
AC代码:
#include<cstdio>
#include<iostream>
#include<algorithm>
#define N 1010
using namespace std;
int n,m,i,j,k,maxx=0,minn=0;
int a[N],b[N];
int main()
{
int sum=0;
cin>>n>>m;
k=n;
for(i=1;i<=m;i++)
{
cin>>a[i];
b[i]=a[i];
sum+=a[i];
}
while(n--)
{
sort(a+1,a+m+1);
maxx+=a[m];//求最大的机票
a[m]--;
}
while(k--)
{
i=1;
int flag=1;
sort(b+1,b+m+1);
while(flag)
{
if(b[i]>0)
{
minn+=b[i];//求最小的机票
b[i]--;
flag=0;
}
i++;
}
}
cout<<maxx<<" "<<minn<<endl;
return 0;
}