CodeForces - 262B

本文探讨了一种通过有限次数的符号变化来最大化公司年度总收入的方法。在一个非递减的整数序列中,通过精确地执行k次数值符号反转,旨在找到使总和达到最大的最优解。文章提供了一个算法解决方案,并通过示例说明了不同场景下如何实现最大化的总收入。
摘要由CSDN通过智能技术生成

 

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;

    }

}

 

转载于:https://www.cnblogs.com/lunatic-talent/p/11386730.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值