CodeForces - 262B
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的更多相关文章
- python爬虫学习(5) —— 扒一下codeforces题面
上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...
- 【Codeforces 738D】Sea Battle(贪心)
http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...
- 【Codeforces 738C】Road to Cinema
http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...
- 【Codeforces 738A】Interview with Oleg
http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...
- CodeForces - 662A Gambling Nim
http://codeforces.com/problemset/problem/662/A 题目大意: 给定n(n <= 500000)张卡片,每张卡片的两个面都写有数字,每个面都有0.5的概 ...
- CodeForces - 274B Zero Tree
http://codeforces.com/problemset/problem/274/B 题目大意: 给定你一颗树,每个点上有权值. 现在你每次取出这颗树的一颗子树(即点集和边集均是原图的子集的连 ...
- CodeForces - 261B Maxim and Restaurant
http://codeforces.com/problemset/problem/261/B 题目大意:给定n个数a1-an(n<=50,ai<=50),随机打乱后,记Si=a1+a2+a ...
- CodeForces - 696B Puzzles
http://codeforces.com/problemset/problem/696/B 题目大意: 这是一颗有n个点的树,你从根开始游走,每当你第一次到达一个点时,把这个点的权记为(你已经到过不 ...
- CodeForces - 148D Bag of mice
http://codeforces.com/problemset/problem/148/D 题目大意: 原来袋子里有w只白鼠和b只黑鼠 龙和王妃轮流从袋子里抓老鼠.谁先抓到白色老鼠谁就赢. 王妃每次 ...
随机推荐
- 过滤器filter学习进度一
过滤器filter他的理解是: filter是对客户端访问资源的过滤,符合条件放行,不符合条件的不放行,并且可以对目标资源访问的前后进行逻辑处理. 快速入门: 1.编写一个过滤器的类实现filter接 ...
- Golang微信支付跳过X509验证
游戏支付提交到微信的post: 微信: https://pay.weixin.qq.com/wiki/doc/api/micropay.php?chapter=23_4 错误信息: err:Post ...
- Java第三十天,I/O操作
一.基本概念 输入输出一般是相对与内存.CPU寄存器.当前进程来说的 输入:从硬盘.键盘等外部设备读取数据到内存.当前进程或CPU寄存器中 输出:利用当前进程将数据写入到硬盘.终端显示屏等外部设备中 ...
- matplotlib 中其他基于Affine2DBase的类
2020-04-11 11:42:22 --Edit by yangray以下所有类(除了BboxTransformToMaxOnly )都直接继承于Affine2DBaseIdentityTrans ...
- java课程设计之--Elasticsearch篇
一.团队课程设计博客链接 https://www.cnblogs.com/Rasang/p/12169899.html 二.个人负责模块或任务说明 2.1Elasticsearch简介 Elastic ...
- AJ学IOS 之微博项目实战(6)导航控制器NavigationController 的滑动回退功能实现
AJ分享,必须精品 一:效果 第二篇里面写了怎样自定义navigation实现自定义的导航控制器左右按钮样式,但是当我们自己实现后,系统自带的向右边滑动来实现回退的功能就不能用了. 这里主要实现滑动回 ...
- ClickOnce的安装路径
win 7下C:\Users\Administrator.U5G4L4PUY34SH5C\AppData\Local\Apps\2.0\KPVZOAYK.0JE\56B55RCH.A7A\winr.. ...
- commonJS、AMD和CMD之间的区别
JS中的模块规范(CommonJS,AMD,CMD),如果你听过js模块化这个东西,那么你就应该听过或CommonJS或AMD甚至是CMD这些规范咯,我也听过,但之前也真的是听听而已. 现在就看看吧, ...
- Teradata 数据库
笔者大学所学计算机专业,读书时接触过Oracle.mysql和SQL SERVER,一度坐井观天觉得数据库应该也就这些了,但自笔者毕业进入数据仓库这个行业,接触的第一个商业数据库即是Teradata, ...
- 文本文件的合并操作方法 - Python
我们有时候,看到几k的日志文件,一大堆,一个一个打开又很麻烦,少看几个,又担心遗漏,这个时候,如果有一个可以合并所有文本文件的工具就好了. 下面这个代码就可以实现,它不局限于.txt格式,基本上字符型 ...