Codeforces 1121 B 枚举计数

http://codeforces.com/problemset/problem/1121/B

Mike decided to teach programming to children in an elementary school. He knows that it is not an easy task to interest children in that age to code. That is why he decided to give each child two sweets.

Mike has nn sweets with sizes a1,a2,…,ana1,a2,…,an . All his sweets have different sizes. That is, there is no such pair (i,j)(i,j) (1≤i,j≤n1≤i,j≤n ) such that i≠ji≠j and ai=ajai=aj .

Since Mike has taught for many years, he knows that if he gives two sweets with sizes aiai and ajaj to one child and akak and apap to another, where (ai+aj)≠(ak+ap)(ai+aj)≠(ak+ap) , then a child who has a smaller sum of sizes will be upset. That is, if there are two children who have different sums of sweets, then one of them will be upset. Apparently, Mike does not want somebody to be upset.

Mike wants to invite children giving each of them two sweets. Obviously, he can't give one sweet to two or more children. His goal is to invite as many children as he can.

Since Mike is busy preparing to his first lecture in the elementary school, he is asking you to find the maximum number of children he can invite giving each of them two sweets in such way that nobody will be upset.

Input

The first line contains one integer nn (2≤n≤10002≤n≤1000 ) — the number of sweets Mike has.

The second line contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤1051≤ai≤105 ) — the sizes of the sweets. It is guaranteed that all integers are distinct.

Output

Print one integer — the maximum number of children Mike can invite giving each of them two sweets in such way that nobody will be upset.

扫描二维码关注公众号,回复: 5474834 查看本文章

Examples

Input

8
1 8 3 11 4 9 2 7

Output

3

Input

7
3 1 7 11 9 2 12

Output

2

Note

In the first example, Mike can give 9+2=119+2=11 to one child, 8+3=118+3=11 to another one, and 7+4=117+4=11 to the third child. Therefore, Mike can invite three children. Note that it is not the only solution.

In the second example, Mike can give 3+9=123+9=12 to one child and 1+111+11 to another one. Therefore, Mike can invite two children. Note that it is not the only solution.

题目大意: 你有n个糖果, 每个糖果有不同的尺寸,你可以从中任意选取2*k个糖果给k个孩子,(每个孩子都可以得到两个糖果), 要求是任意所有孩子得到的糖果的尺寸之和相等。 输入保证所有糖果的尺寸都是不相等的。

思路: O(n^2)可以枚举出所有的情况,(比如选第1个和第2个 第1个和第3个……第1个和第n个 第2个和第3个……)同时计数统计凑成该数的最大方案数, 然后输出记录的最大值即可。解释一下这样做为什么不会重复选择,因为题目说的那个所有糖果的尺寸都是不相等的条件,因此若a+b=k,必定不可能存在另外一个c使得a+c=k成立,因此不会有重复的选择。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#define INF 0x3f3f3f3f
typedef long long ll;
using namespace std;

const int maxn=100000;

int vis[maxn<<1+5];
int a[1005];

int main()
{
    int n;
    scanf("%d",&n);
    for(int i=0;i<n;i++)
        scanf("%d",&a[i]);
    int MAX=0;
    for(int i=0;i<n-1;i++)
    {
        for(int j=i+1;j<n;j++)
        {
            ++vis[a[i]+a[j]];
            MAX=max(MAX,vis[a[i]+a[j]]);
        }
    }
    printf("%d\n",MAX);
    return 0;
};

猜你喜欢

转载自blog.csdn.net/xiji333/article/details/88149429