题目链接

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;
}

CodeForces-1061B Views Matter的更多相关文章

  1. Codeforces Round #523 (Div. 2) B Views Matter

    传送门 https://www.cnblogs.com/violet-acmer/p/10005351.html 这是一道贪心题么???? 题意: 某展览馆展览一个物品,此物品有n堆,第 i 堆有a[ ...

  2. B. Views Matter

    链接 [http://codeforces.com/contest/1061/problem/B] 题意 问你最多去掉多少块使得从上和右看,投影图不变 分析 注意细节,尤其第一列 代码 #includ ...

  3. 【贪心】【CF1061B】 Views Matter

    Description 给定一个只有一行的,由 \(n\) 组小正方体组成的方块,每组是由 \(a_i\) 块小正方体竖直堆叠起来的,求最多能抽掉多少块使得它的左视图和俯视图不变.方块不会掉落 Inp ...

  4. CF1061B Views Matter

    思路: 贪心. 实现: #include <bits/stdc++.h> using namespace std; ]; int main() { int n, m; while (cin ...

  5. Codeforces Round #523 (Div. 2)

    Codeforces Round #523 (Div. 2) 题目一览表 来源 考察知识点 完成时间 A Coins cf 贪心(签到题) 2018.11.23 B Views Matter cf 思 ...

  6. Codeforces Round #523 (Div. 2) Solution

    A. Coins Water. #include <bits/stdc++.h> using namespace std; int n, s; int main() { while (sc ...

  7. Codeforces 528D Fuzzy Search(FFT)

    题目 Source http://codeforces.com/problemset/problem/528/D Description Leonid works for a small and pr ...

  8. Codeforces Round #313 (Div. 2)B.B. Gerald is into Art

    B. Gerald is into Art Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/problemset/ ...

  9. CodeForces 445B DZY Loves Chemistry

    DZY Loves Chemistry Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64 ...

随机推荐

  1. Python 爬虫的工具列表

    这个列表包含与网页抓取和数据处理的Python库 网络 通用 urllib -网络库(stdlib). requests -网络库. grab - 网络库(基于pycurl). pycurl - 网络 ...

  2. 如何安装Ruby(Windows)

    Ruby解释器的安装 1.Windows平台 想尽快安装并运行Ruby,可遵循如下步骤: 1.启动Web浏览器,访问 http://www.ruby-lang.org/en/downloads/ 2. ...

  3. 《C++总结3》

    派生类 Class student1:public student   //表示公用继承,默认为私有的 {  public : …… …… } 继承的时候一定是全部继承来,但是可以自己设定访问属性,或 ...

  4. Vscode插件--微信小程序格式化以及高亮组件wxml-vscode

    wxml-vscode wxml-vscode 仓库 提问题 安装 通过 F1 或者 CMD + Shift + P 输入 install. 选择: Install Extension. 特性 格式化 ...

  5. phonegap二维码扫描插件

    原文出处:http://rensanning.iteye.com/blog/2034026 谈谈我使用这个的体会吧; git地址 https://github.com/wildabeast/Barco ...

  6. 给网站添加icon图标

    只需制成ico结尾的图片即可

  7. linux安装openldap步骤

    目录 虚拟机环境:centos 7 一.环境准备 1.关闭 selinux firewalld 临时: setenforce 0  永久: vi /etc/sysconfig/selinux SELI ...

  8. ubuntu修改IP地址和网关的方法

    一.使用命令设置Ubuntu IP地址 1.修改配置文件blacklist.conf禁用IPV6 sudo vi /etc/modprobe.d/blacklist.conf 表示用vi编辑器(也可以 ...

  9. LeetCode207 课程表

    问题:课程表 现在你总共有 n 门课需要选,记为 0 到 n-1. 在选修某些课程之前需要一些先修课程. 例如,想要学习课程 0 ,你需要先完成课程 1 ,我们用一个匹配来表示他们: [0,1] 给定 ...

  10. (servlet页面跳转没有反应)

    问题:页面跳转到/UserManager/LoginCLServlet,就一直没有反应,无法继续执行下去(servlet页面跳转没有反应) 解决: doPost()方法里面必须写成这样 正确的写法:  ...