CodeForces 1061B 贪心

http://codeforces.com/problemset/problem/1061/B

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 nn and mm (1≤n≤100000, 1≤m≤10^9) — the number of stacks and the height of the exhibit.

The second line contains nn 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 10 blue cells, so the answer is 10 .

题目大意:一堆方块,让你取出最大数量的方块,保证从上面看或从右面看没有变化。还不懂的话,看个样题吧。

思路:贪心,读入n个位置的方块个数,排序后开始遍历。最优的策略是,不管前n-1堆有多高,每一堆都只放一个方块,这样肯定可以保证前i-1堆从上面看是没有变化的。遍历的同时用h来记录当前的高度。即h<a[i]则++h。然后考虑第n堆怎么取来保证右视图没有变化,分两种情况:(1)h=a[n],那么这堆留下一个就可以了。(2)h<a[n],那么在高度h以上的方块都不能取走,,因此加上h-1即可。

#include<iostream>
#include<cstdio>
#include<cmath>
#include<set>
#include<algorithm>
#include<iterator>
#define EPS 1e-12
#define INF 0x3f3f3f3f
#define MOD 1000000007
typedef long long ll;
using namespace std;

ll a[100005];

int main()
{
	int n,m;
	scanf("%d %d",&n,&m);
	for(int i=0;i<n;++i)
		scanf("%lld",&a[i]);
	if(n==1)
	{
		printf("0\n");
		return 0;
	}
	sort(a,a+n);
	ll cnt=0;
	ll h=0;
	for(int i=0;i<n;++i)
	{
		if(i!=n-1)
			cnt+=a[i]-1;
		if(h<a[i])
			h++;
	}
	if(h<a[n-1])
		cnt+=h-1;
	else
		cnt+=a[n-1]-1;
	printf("%lld\n",cnt);
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值