Codeforces 368A Sereja and Coat Rack(模拟)

题目链接:Codeforces 368A Sereja and Coat Rack

直接模拟,简单题。

#include <iostream>
#include <algorithm>

using namespace std;

const int MAX_N = 100 + 10;
int a[MAX_N];
int n,d,m;

int main()
{
    int sum = 0;
    cin >> n >> d;
    for(int i = 1;i <= n;i++)
    {
        cin >> a[i];
        sum += a[i];
    }
    sort(a + 1,a + n + 1);
    cin >> m;
    if(n == m)
        cout << sum << endl;
    else if(m > n)
        cout << sum - (m - n) * d << endl;
    else
    {
        sum = 0;
        for(int i = 1;i <= m;i++)
            sum += a[i];
        cout << sum << endl;
    }
    return 0;
}



你可能感兴趣的:(Codeforces 368A Sereja and Coat Rack(模拟))