CodeforcesAug 27, 2025

Views Matter

Hazrat Ali

Codeforces

The height of the exhibit is equal to m. Consequently, the number of blocks in each stack is less than or equal to m.

There is a camera on the ceiling that sees the top view of the blocks and a camera on the right wall that sees the side view of the blocks.

Find the maximum number of blocks you can remove such that the views for both the cameras would not change.

Note, that while originally all blocks are stacked on the floor, it is not required for them to stay connected to the floor after some blocks are removed. There is no gravity in the whole exhibition, so no block would fall down, even if the block underneath is removed. It is not allowed to move blocks by hand either.

Input

The first line contains two integers n and m (1n1000001m109) — the number of stacks and the height of the exhibit.

The second line contains n integers a1,a2,,an (1aim) — the number of blocks in each stack from left to right.

Output

Print exactly one integer — the maximum number of blocks that can be removed.

Examples
Input
5 6
3 3 3 3 3
Output
10
Input
3 5
1 2 4
Output
3
Input
5 5
2 3 1 4 4
Output
9
Input
1 1000
548
Output
0
Input
3 3
3 1 1
Output
1

Solution

#include <bits/stdc++.h>
using namespace std;

int main()
{
    int n, m;
    cin >> n >> m;
    vector<int> s(n);
    long long sum = 0;
    int mx = 1;
    for (int i = 0; i < n; i++)
    {
        cin >> s[i];
        sum += s[i];
        mx = max(mx, s[i]);
    }
    sort(s.begin(), s.end());
    int ans = sum - max(mx, n);
    if (mx >= n and n > 1 and s[1] == 1)
    {
        ans--;
    }
    cout << ans << endl;
    return 0;
}





Comments