In the evening, after the contest Ilya was bored, and he really felt like maximizing. He remembered that he had a set of n sticks and an instrument. Each stick is characterized by its length li.
Ilya decided to make a rectangle from the sticks. And due to his whim, he decided to make rectangles in such a way that maximizes their total area. Each stick is used in making at most one rectangle, it is possible that some of sticks remain unused. Bending sticks is not allowed.
Sticks with lengths a1, a2, a3 and a4 can make a rectangle if the following properties are observed:
- a1 ≤ a2 ≤ a3 ≤ a4
- a1 = a2
- a3 = a4
A rectangle can be made of sticks with lengths of, for example, 3 3 3 3 or 2 2 4 4. A rectangle cannot be made of, for example, sticks 5 5 5 7.
Ilya also has an instrument which can reduce the length of the sticks. The sticks are made of a special material, so the length of each stick can be reduced by at most one. For example, a stick with length 5 can either stay at this length or be transformed into a stick of length 4.
You have to answer the question — what maximum total area of the rectangles can Ilya get with a file if makes rectangles from the available sticks?
The first line of the input contains a positive integer n (1 ≤ n ≤ 105) — the number of the available sticks.
The second line of the input contains n positive integers li (2 ≤ li ≤ 106) — the lengths of the sticks.
The first line of the output must contain a single non-negative integer — the maximum total area of the rectangles that Ilya can make from the available sticks.
4
2 4 4 2
8
4
2 2 3 5
0
4
100003 100004 100005 100006
10000800015
给出n条线段的长度,任意一条长度为len的线段可以当作len或len-1的线段使用,求能构成的矩形的最大的总面积。
任意一个矩形当中有两对边的长度是相等的。我们将问题转化为 对子问题。 要是总面积最大,就要贪心,使长度最大的对子和长度次最大的对子 组合,接下去同样的组合。 代码实现如下。
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <string> 7 #include <vector> 8 #include <set> 9 #include <map> 10 #include <queue> 11 #include <stack> 12 #include <sstream> 13 #include <iomanip> 14 using namespace std; 15 typedef long long LL; 16 const int INF=0x4fffffff; 17 const int EXP=1e-6; 18 const int MS=100005; 19 const int MS2=1000005; 20 21 int a[MS]; 22 23 24 int main() 25 { 26 int n; 27 scanf("%d",&n); 28 for(int i=0;i<n;i++) 29 scanf("%d",&a[i]); 30 sort(a,a+n); 31 LL ans=0,l=0; 32 for(int i=n-1;i>0;) 33 { 34 if(a[i]-a[i-1]==1||a[i]==a[i-1]) 35 { 36 if(l==0) 37 l=a[i-1]; 38 else 39 { 40 ans+=l*a[i-1]; 41 l=0; 42 } 43 i-=2; 44 } 45 else 46 i--; 47 } 48 printf("%lld ",ans); 49 return 0; 50 }