Codeforces Gym101522 A. Ambiguous Dates (La Salle-Pui Ching Programming Challenge 培正喇沙編程挑戰賽 2017)

 

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 ≤ 1051 ≤ 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



代码:

 1 #include<iostream>
 2 #include<cstring>
 3 #include<cstdio>
 4 #include<algorithm>
 5 using namespace std;
 6 typedef long long ll;
 7 const int N=1e5+10;
 8 int a[N];
 9 int main(){
10     int n;
11     ll ans;
12     while(~scanf("%d",&n)){
13         for(int i=1;i<=n;i++)
14             scanf("%d",&a[i]);
15         sort(a+1,a+1+n);
16         ans=0;
17         for(int i=1;i<=n;i++){
18             if(a[i]>n){if(n-i>0)ans+=n-i;}
19             if(a[i]<n){if(a[i]-i>0)ans+=a[i]-i;}
20             if(a[i]==n){if(n-i>0)ans+=n-i;}
21         }
22         ans*=2;
23         printf("%lld\n",ans);
24     }
25     return 0;
26 }

 

 

 

 

 

 



posted @ 2017-12-17 20:37  ZERO-  阅读(550)  评论(0编辑  收藏  举报