A. Ambiguous Dates

There are two popular formats for representing a date: day/month/year or month/day/year. For example, today can be represented as 15/8/2017 or 8/15/2017.

Sometimes (like on today), using one way or another should pose no confusion — it is immediately understood that the date is the 15th of August. On other days, however, the two representations may be interpreted as two different valid dates. For example, the 7th of August may be misinterpreted as the 8th of July, since both can be represented as 7/8/2017 (or 8/7/2017).

We say a date (D, M, Y) is ambiguous if D/M/Y and M/D/Y, when both interpreted in the day/month/year format, are different valid dates. For example, (7, 8, 2017) and (8, 7, 2017) are ambiguous, while (15, 8, 2017) and (10, 10, 2017) are not.

The total number of ambiguous dates in the Gregorian calendar system on any given year is equal to 12 × 11 = 132.

Now, suppose that in a hypothetical calendar system, there are M months, where the i-th month has D[i] days, numbered from 1 to D[i]. Assume that there are no leap years.

You are to carry out a calendar reform, by shuffling the array D[], and your target is to minimize the total number of ambiguous dates in a calendar year. Specifically, you want to find a permutation p[1], p[2], ..., p[M] of integers 1, 2, ..., M, such that the new calendar system, where the i-th month has D[p[i]] days, has the minimal number of ambiguous dates. Output that minimal number.

Input

The first line of input consists of a single integer M, the number of months in the hypothetical calendar system.

The second line of input consists of M integers D[1], D[2], ..., D[M], the original number of days in the i-th month.

For all test cases, 1 ≤ M ≤ 105, 1 ≤ D[i] ≤ 105.

Output

Output a single integer, the minimal number of ambiguous dates after the calendar reform.

Example

Input
12
31 28 31 30 31 30 31 31 30 31 30 31
Output
132
Input
3
5 1 1
Output
0

代码:

 #include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
typedef long long ll;
const int N=1e5+;
int a[N];
int main(){
int n;
ll ans;
while(~scanf("%d",&n)){
for(int i=;i<=n;i++)
scanf("%d",&a[i]);
sort(a+,a++n);
ans=;
for(int i=;i<=n;i++){
if(a[i]>n){if(n-i>)ans+=n-i;}
if(a[i]<n){if(a[i]-i>)ans+=a[i]-i;}
if(a[i]==n){if(n-i>)ans+=n-i;}
}
ans*=;
printf("%lld\n",ans);
}
return ;
}

Gym101522A Gym101522C Gym101522D的更多相关文章

随机推荐

  1. 串String(2):串的实现(堆分配存储表示法)

    7/27/2017,先占个位,最近事情比较忙,明天敲一波代码,预测在一星期内搞定 9/02/2017,看到这个十分汗颜,八月去美帝学习了,没有抽空补上这一博文,计划这个月开了数据结构课后补上

  2. scala写算法-从后缀表达式构造

    一个例子,比如ab+cde+**,这是一个后缀表达式,那么如何转换为一棵表达式树呢? 先上代码,再解释: object Main extends App{ import Tree.node def i ...

  3. lua 批量重命名文件

    local s = io.popen("dir F:\\headicon /b/s") local filelist = s:read("*all") loca ...

  4. Java 读者写者问题

    实验存档.V 允许好几个人同时读,但是不允许在有人读的时候写,以及同一时间只能有一个人在写. 读者.java: package operating.entity.readerwriter; impor ...

  5. 4、树莓派的中文:安装ftp,安装gcc,安装qt,编译qt程序,运行qt界面程序

    本博文仅作本人操作过程的记录,留作备忘.自强不息 QQ1222698 1.安装FTP:sudo apt-get install vsftpd 2.配置FTP,修改,/etc/vsftpd.conf # ...

  6. vue常见错误及解决办法

    1.在配置路由并引入组件后,报错: Unknown custom element: <router-link> - did you register the component corre ...

  7. HBase资料

    http://blog.csdn.net/ymh198816/article/details/51244911 https://www.cnblogs.com/JingJ/p/4521245.html ...

  8. Spring加载XML机制

    转载自跳刀的兔子   http://www.cnblogs.com/shipengzhi/articles/3029872.html 加载文件顺序 情形一:使用classpath加载且不含通配符 这是 ...

  9. Linux : screen 工具详解

    转自:http://www.cnblogs.com/mchina/archive/2013/01/30/2880680.html [ Screen 官方站点 ] 背景 系统管理员经常需要SSH 或者t ...

  10. Netty之ProtoBuf(六)

    Protocol Buffer的基本使用(六) 一.简介 Protocol Buffer(简称ProtoBuf)是google的一个语言中立,平台中立,可扩展的对结构化的数据进行序列化的一种机制,和X ...