第一行输入一个整数
。
第二行输入
个整数
。
输出一个整数,表示满足条件的最大偶数和。
3 2 5 6
8
当灵异背包里面有,此时总和为
,为奇数,不满足条件。
当灵异背包里面有,此时总和为
,为偶数,且为最大值。
1 3
0
选择的灵异背包为空,总和为0。
const rl = require("readline").createInterface({ input: process.stdin });
var iter = rl[Symbol.asyncIterator]();
const readline = async () => (await iter.next()).value;
void async function () {
// Write your code here
while(line = await readline()){
const n = Number(line);
const nums = (await readline()).split(' ').map(Number);
let sum = 0;
let odds = [];
for (let i = 0; i < n; i++) {
if (nums[i] % 2 === 0) {
sum += nums[i];
} else {
odds.push(nums[i]);
}
}
if (odds.length > 1) {
odds.sort((a, b) => b - a);
const len = odds.length % 2 === 0 ? odds.length : odds.length-1;
for (let i = 0; i < len; i++) {
sum += odds[i];
}
}
console.log(sum);
}
}()