Codeforces Round #182 (Div. 2)---C. Yaroslav and Sequence(不错的题,分析找规律)

Yaroslav has an array, consisting of (2·n?-?1) integers. In a single operation Yaroslav can change the sign of exactly n elements in the array. In other words, in one operation Yaroslav can select exactly n array elements, and multiply each of them by -1.

Yaroslav is now wondering: what maximum sum of array elements can be obtained if it is allowed to perform any number of described operations?

Help Yaroslav.

Input

The first line contains an integer n (2?≤?n?≤?100). The second line contains (2·n?-?1) integers — the array elements. The array elements do not exceed 1000 in their absolute value.

Output

In a single line print the answer to the problem — the maximum sum that Yaroslav can get.

Sample test(s)

Input

2

50 50 50

Output

150

Input

2

-1 -100 -1

Output

100

Note

In the first sample you do not need to change anything. The sum of elements equals 150.

In the second sample you need to change the sign of the first two elements. Then we get the sum of the elements equal to 100.

2*n-1个数,每次可以选择n个数,改变它们的符号

可以发现,当n是奇数,我们可以令任意数目的数改变符号

当n是偶数,我们可以使得2*k对数改变符号,这里枚举下求个最大值就行

/*************************************************************************
    > File Name: cf-182-c.cpp
    > Author: ALex
    > Mail: [email protected]
    > Created Time: 2015年05月06日 星期三 20时22分21秒
 ************************************************************************/

#include <functional>
#include <algorithm>
#include <iostream>
#include <fstream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <queue>
#include <stack>
#include <map>
#include <bitset>
#include <set>
#include <vector>

using namespace std;

const double pi = acos(-1.0);
const int inf = 0x3f3f3f3f;
const double eps = 1e-15;
typedef long long LL;
typedef pair <int, int> PLL;

int arr[220];

int main() {
    int n;
    while (~scanf("%d", &n)) {
        int cnt1 = 0, cnt2 = 0;
        for (int i = 1; i <= 2 * n - 1; ++i) {
            scanf("%d", &arr[i]);
            if (arr[i] < 0) {
                ++cnt1;
            }
            else {
                ++cnt2;
            }
        }
        sort(arr + 1, arr + 2 * n);
        if (n & 1) {
            int sum = 0;
            for (int i = 1; i <= 2 * n - 1; ++i) {
                sum += abs(arr[i]);
            }
            printf("%d\n", sum);
        }
        else {
            int sum = 0;
            if (cnt1 & 1) {
                for (int k = 0; 2 * k <= 2 * n - 1; ++k) {
                    int tmp = 0;
                    for (int i = 1; i <= 2 * n - 1; ++i) {
                        if (i <= k * 2) {
                            tmp -= arr[i];
                        }
                        else {
                            tmp += arr[i];
                        }
                    }
                    sum = max(sum, tmp);
                }
            }
            else {
                for (int i = 1; i <= 2 * n - 1; ++i) {
                    sum += abs(arr[i]);
                }
            }
            printf("%d\n", sum);
        }
    }
    return 0;
}
时间: 2024-10-10 06:23:19

Codeforces Round #182 (Div. 2)---C. Yaroslav and Sequence(不错的题,分析找规律)的相关文章

Codeforces Round #258 (Div. 2) B. Sort the Array(简单题)

题目链接:http://codeforces.com/contest/451/problem/B ---------------------------------------------------------------------------------------------------------------------------------------------------------- 欢迎光临天资小屋:http://user.qzone.qq.com/593830943/ma

Codeforces Round #404 (Div. 2) C 二分,水 D 数学,好题 E 分块

Codeforces Round #404 (Div. 2) C. Anton and Fairy Tale 题意:仓库容量n,每天运来m粮食,第 i 天被吃 i 粮食,问第几天仓库第一次空掉. tags:==SB题 注:二分边界判断,数据范围爆long long判断. // CF404 C #include<bits/stdc++.h> using namespace std; #pragma comment(linker, "/STACK:102400000,102400000&

Codeforces Round #179 (Div. 2) B. Yaroslav and Two Strings (容斥原理)

题目链接 Description Yaroslav thinks that two strings s and w, consisting of digits and having length n are non-comparable if there are two numbers, i andj(1 ≤ i, j ≤ n), such that si > wi and sj < wj. Here sign si represents the i-th digit of string s,

Codeforces Round #257 (Div. 2) A. Jzzhu and Children(简单题)

题目链接:http://codeforces.com/problemset/problem/450/A ---------------------------------------------------------------------------------------------------------------------------------------------------------- 欢迎光临天资小屋:http://user.qzone.qq.com/593830943

Codeforces Round #350 (Div. 2) F. Restore a Number 模拟构造题

F. Restore a Number Vasya decided to pass a very large integer n to Kate. First, he wrote that number as a string, then he appended to the right integer k — the number of digits in n. Magically, all the numbers were shuffled in arbitrary order while

Codeforces Round #350 (Div. 2) E. Correct Bracket Sequence Editor

E. Correct Bracket Sequence Editor Recently Polycarp started to develop a text editor that works only with correct bracket sequences (abbreviated as CBS). Note that a bracket sequence is correct if it is possible to get a correct mathematical express

Codeforces Round #350 (Div. 2) E. Correct Bracket Sequence Editor 线段树模拟

E. Correct Bracket Sequence Editor Recently Polycarp started to develop a text editor that works only with correct bracket sequences (abbreviated as CBS). Note that a bracket sequence is correct if it is possible to get a correct mathematical express

Codeforces Round #342 (Div. 2) D. Finals in arithmetic(想法题/构造题)

传送门 Description Vitya is studying in the third grade. During the last math lesson all the pupils wrote on arithmetic quiz. Vitya is a clever boy, so he managed to finish all the tasks pretty fast and Oksana Fillipovna gave him a new one, that is much

Codeforces Round #174 (Div. 2)---C. Cows and Sequence(操作序列)

Bessie and the cows are playing with sequences and need your help. They start with a sequence, initially containing just the number 0, and perform n operations. Each operation is one of the following: Add the integer xi to the first ai elements of th