Problem Statement
You are given a sequence A=(A_1,A_2,\ldots,A_N)A=(A1,A2,…,A**N) of length NN consisting of non-negative integers.
Determine if there is an even number represented as the sum of two different elements of AA. If it exists, find the maximum such number.
Constraints
-
2\leq N \leq 2\times 10^52≤N≤2×105
-
0\leq A_i\leq 10^90≤A**i≤109
-
The elements of AA are distinct.
-
All values in the input are integers.
Input
The input is given from Standard Input in the following format:
N A_1A 1 • A_2A 2 • \ldots… A_NA N •
Output
Print -1
if there is no even number represented as the sum of two different elements of AA.
If such an even number exists, print the maximum such number.
Sample 1
Inputcopy | Outputcopy |
---|---|
3 2 3 4 | 6 |
The values represented as the sum of two distinct elements of AA are 55, 66, and 77. We have an even number here, and the maximum is 66.
Sample 2
Inputcopy | Outputcopy |
---|---|
2 1 0 | -1 |
The value represented as the sum of two distinct elements of AA is 11. We have no even number here, so -1
should be printed.
题意www:
输入整数n,紧接着输入n个整数。各个整数两两相加,输出这些和当中最大的偶数和。
代码www:
#include <cstdio>
#include <cmath>
#include <iostream>
#include <algorithm>
using namespace std;
int n;
const int N=2e5+5; //开数组的时候可能需要const来转变一下昂;***
int a[N];
int b[N];
int c;
int conta=0,contb=0;
int main() {
cin>>n;
for(int i=1; i<=n; i++) {
cin>>c;
if(c&1) { //把这些数分为奇数和偶数分别存起来,会发现规律的昂(奇+奇=偶、偶+偶=奇);***
a[conta]=c;
conta++;
} else {
b[contb]=c; //在这里,要学会巧妙地敲入数组下标昂!
contb++;
}
}
sort(a,a+conta+1);
sort(b,b+contb+1); //这里为什么要加一捏?(思考思考,回头补上答案嗷)
int sum=-1;
if(conta>=2) { //这里的>=2别有洞天嗷,注意观察并动笔写一写嗷;
sum=max(sum,a[conta-1]+a[conta]); //注意昂,可能奇数之和要比偶数之和大,但是这里,在奇数那里会进一步比较sum哒,所以不用担心吼吼吼;
}
if(contb>=2) {
sum=max(sum,b[contb-1]+b[contb]);
}
cout<<sum<<'\n';
return 0;
}
okk,一篇blog完成啦啦啦o(* ̄▽ ̄*)ブ