码迷,mamicode.com
首页 > 其他好文 > 详细

CodeForces - 262B

时间:2019-08-21 09:44:45      阅读:78      评论:0      收藏:0      [点我收藏+]

标签:min   ret   tip   max   constant   enc   def   seq   ble   

 

Roma works in a company that sells TVs. Now he has to prepare a report for the last year.

Roma has got a list of the company‘s incomes. The list is a sequence that consists of n integers. The total income of the company is the sum of all integers in sequence. Roma decided to perform exactly k changes of signs of several numbers in the sequence. He can also change the sign of a number one, two or more times.

The operation of changing a number‘s sign is the operation of multiplying this number by -1.

Help Roma perform the changes so as to make the total income of the company (the sum of numbers in the resulting sequence) maximum. Note that Roma should perform exactlyk changes.

Input

The first line contains two integers n and k (1 ≤ n, k ≤ 105), showing, how many numbers are in the sequence and how many swaps are to be made.

The second line contains a non-decreasing sequence, consisting of n integers ai(|ai| ≤ 104).

The numbers in the lines are separated by single spaces. Please note that the given sequence is sorted in non-decreasing order.

Output

In the single line print the answer to the problem — the maximum total income that we can obtain after exactly k changes.

Examples

Input
3 2
-1 -1 1
Output
3
Input
3 1
-1 -1 1
Output
1

Note

In the first sample we can get sequence [1, 1, 1], thus the total income equals 3.

In the second test, the optimal strategy is to get sequence [-1, 1, 1], thus the total income equals 1.

 

变K次,而且本来数组就是有序的,想让最早的负数都变成,然后就只有如下可能:

1.K次变换结束后,仍存在负数

2.K次变换结束后,恰好无负数

3.K次变换未完成,无负数

前两种直接求和,因为每次对最小值取反,一定是最优解。

第三种,如果数组中有0,或者 剩余K为偶数,那么最优是保持原来数组的大小即为最优,K次全部作用于偶数,或作用于零,不改变数组大小。

另外一种情况,K次操作后必然至少会有一个值被取反,所以一定是最小的数字,时间允许,直接排序,干就完事。

 

#include<iostream>
#include<queue>
#include<algorithm>
#include<set>
#include<cmath>
#include<vector>
#include<map>
#include<stack>
#include<bitset>
#include<cstdio>
#include<cstring>
//---------------------------------Sexy operation--------------------------//

#define cini(n) scanf("%d",&n)
#define cinl(n) scanf("%lld",&n)
#define cinc(n) scanf("%c",&n)
#define cins(s) scanf("%s",s)
#define coui(n) printf("%d",n)
#define couc(n) printf("%c",n)
#define coul(n) printf("%lld",n)
#define speed ios_base::sync_with_stdio(0)
#define file  freopen("input.txt","r",stdin);freopen("output.txt","w",stdout)
//-------------------------------Actual option------------------------------//

#define Swap(a,b) a^=b^=a^=b
#define Max(a,b) a>b?a:b
#define Min(a,b) a<b?a:b
#define mem(n,x) memset(n,x,sizeof(n))
#define mp(a,b) make_pair(a,b)
//--------------------------------constant----------------------------------//

#define INF  0x3f3f3f3f
#define maxn  100010
#define esp  1e-9
using namespace std;
typedef long long ll;
typedef pair<int,int> PII;
//------------------------------Dividing Line--------------------------------//
int n,m;
int a[maxn];
int main()
{
    cini(m),cini(n);
    int t=0;
    for(int i=0;i<m;i++)
    {
        cin>>a[i];
        if(a[i]<0){
            if(a[i]==0) t=i;
            if(n)
            {
                a[i]=-a[i];
                n--;
            }
        }

    }
    long long ans=0;
    if(t==0&&n!=0&&n%2==1)
    {
        sort(a,a+m);
        a[0]=-a[0];
        for(int i=0;i<m;i++) ans+=a[i];
        cout<<ans<<endl;
        return 0;

    }
    else {
         for(int i=0;i<m;i++) ans+=a[i];
        cout<<ans<<endl;
        return 0;

    }

}

 

CodeForces - 262B

标签:min   ret   tip   max   constant   enc   def   seq   ble   

原文地址:https://www.cnblogs.com/lunatic-talent/p/11386730.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!