题目链接
https://vjudge.net/problem/CodeForces-1061B
题面
Description
You came to the exhibition and one exhibit has drawn your attention. It consists of nn stacks of blocks, where the ii-th stack consists of aiai blocks resting on the surface.
The height of the exhibit is equal to mm. Consequently, the number of blocks in each stack is less than or equal to mm.
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 mm (1≤n≤100000, 1≤m≤(10^9)) — the number of stacks and the height of the exhibit.
The second line contains n integers a1,a2,…,an (1≤ai≤m) — 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
Note
The following pictures illustrate the first example and its possible solution.
Blue cells indicate removed blocks. There are 1010 blue cells, so the answer is 1010.
题意
给定n组木块,每组有a[i]个木块,问可以取走多少块木块使这几组木块的右视图和俯视图不变,拿走木块后其他木块不会因为重力下落
题解
我们可以先排序,然后就直接贪心的选取即可,一开始从最底下的一块开始选,如果当前可以向上走那就向上走(即走斜线选取),不能的话就可以只保留一块木块其他的全拿走,对于最后一组,判断一下之前最高走到多高,如果最后一组的高度大于之前走到高度,那么最后一组就至少要保留这个高度之上的木块,否则保留一块即可
AC代码
#include <iostream>
#include <cstdio>
#include <algorithm>
#define N 100050
using namespace std;
int n, m;
int a[N];
int main() {
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
}
sort (a + 1, a + n + 1);
if (n == 1) {
cout << 0;
return 0;
}
int now = 1;
long long ans = a[1] - now;
for (int i = 2; i < n; i++) {
if (a[i] > now) {
now++;
ans += a[i] - 1;
}
else {
ans += a[i] - 1;
}
}
if (a[n] > now) ans += now;
else {
ans += a[n] - 1;
}
cout << ans;
return 0;
}