Views Matter(模拟)

在展览中,有一个由n堆方块组成的展品,每堆高度不超过m。目标是在不改变顶部和侧面视角的情况下,找出最多能移除多少个方块。题目提供了输入输出示例及解析,涉及到对上视图和侧视图保持不变的条件分析。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

B. Views Matter

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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 aiaiblocks 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≤1000001≤n≤100000, 1≤m≤1091≤m≤109) — the number of stacks and the height of the exhibit.

The second line contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤m1≤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

Copy

5 6
3 3 3 3 3

output

Copy

10

input

Copy

3 5
1 2 4

output

Copy

3

input

Copy

5 5
2 3 1 4 4

output

Copy

9

input

Copy

1 1000
548

output

Copy

0

input

Copy

3 3
3 1 1

output

Copy

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.

题意:

获得正视图

求不改变左视图和上视图的情况下,方块可以减少多少个(方块可以悬空)

解析:

sum记录已经加的方块

asum记录竖面已经完成的方块(未重叠的)

bsum记录总数目

if(asum<a[i])     //竖面不能无限记录,要求不大于最高的

先每堆都要放一个(保证上视图),记录竖面完成的数目asum

还需要(maxh-asum)

结果:   bsum - (maxh-asum+sum)

ac:

#include<bits/stdc++.h>
#define MAXN 100005
#define ll long long
using namespace std;

int a[MAXN]={0};


int main()
{
    int n,m,qsum=0;
    ll sum=1,asum=1,bsum=0;
    scanf("%d%d",&n,&m);
    for(int i=1;i<n+1;i++)
    {
        scanf("%d",&a[i]);
        bsum+=a[i];
    }
    if(n==1)
        printf("0\n");
    else{
        sort(a+1,a+n+1);
        for(int i=2;i<n+1;i++)
        {
            if(a[i]!=a[i-1])
            {
                sum++;
                asum++;
            }
            else{
                if(asum<a[i])
                {
                    sum++;
                    asum++;
                }
                else sum++;
            }
            qsum=max(a[i],qsum);
        }
        cout<<bsum-(qsum-asum+sum)<<endl;
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值