题目链接

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. P1316 丢瓶盖

    题目描述 陶陶是个贪玩的孩子,他在地上丢了A个瓶盖,为了简化问题,我们可以当作这A个瓶盖丢在一条直线上,现在他想从这些瓶盖里找出B个,使得距离最近的2个距离最大,他想知道,最大可以到多少呢? 输入输出 ...

  2. 2017.11.1 微型计算机原理与接口技术-----第七章 中断系统与8237A DMA控制器

    第七章 微型计算机原理与接口技术-----中断系统与8237A DMA控制器 (1)数据传送的两种方式:中断方式和直接存储器存取方式(DMA):中断是微处理器与外部设备交换信息的一种方式:DMA是存储 ...

  3. c#中的 MessageBox 弹出提示框的用法

    MessageBox.Show(<字符串str> Text, <字符串str> Title, <整型int> nType,MessageBoxIcon); 例:Me ...

  4. ID3和C4.5、CART

    CART连续属性参考C4.5的离散化过程,区别在于CART算法中要以GiniGain最小作为分界点选取标准.是否需要修正?处理过程为: 先把连续属性转换为离散属性再进行处理.虽然本质上属性的取值是连续 ...

  5. ajax实现分页页签

    在一些搜索列表的页面中,我们会遇到一些需要处理页签的需求,一般这样的页面,要么是在JSP中处理,每次都跳页.这样做是个很方便的方法.但是如果页面上有很多和列表无关,每次都需要重新渲染是不是显得慢了一些 ...

  6. XML DTD约束 对xml文件的crud的查询Read Retrieve操作 xml递归遍历

    本地的dtd文档 xml中引入dtd文档 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE 书 ...

  7. JQuery实现父级选择器(广告实现)

    效果图如下: HTML代码如下: <!DOCTYPE html> <html lang="en"> <head> <meta charse ...

  8. nginx+php-fpm结构模型剖析及优化(转载)

    一.nginx和php-fpm的关系和分工 nginx是web服务器,php-fpm是一个PHPFastCGI进程管理器,两者遵循fastcgi的协议进行通信,nginx负责静态类似html文件的处理 ...

  9. .Net Core爬虫爬取妹子网图片

    现在网上大把的Python的爬虫教程,很少看见有用C#写的,正好新出的.Net Core可以很方便的部署到Linux上,就用妹子图做示范写个小爬虫 在C#下有个很方便的类库 HtmlAgilityPa ...

  10. centOS初了解--***安装node

    在***买了一个VPS,用了差不多一年了,除了做FQ使用之外,同时也下载了一个node,用了express搭建了一个服务,同时我在博客园有博客,我也懒得转来转去了,直接做了一个重定向,跳转到了博客园. ...