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;
}
}
- Eclipse中JSP生成的class文件去了哪里?
- http-server 超轻量级web服务器
- JSP Session管理
- 明年4月1日起扫码付款将限额 最低单日限额500元
- 支付宝、微信支付即将限额?这让出门不带钱包的我怎么过!
- 王者荣耀玩家的福音 区块链技术可以让游戏资产变成真实资产
- 不知道这些的IT人可能白过了2017!
- 什么情况?又出新规定,微信支付和支付宝支付居然要被限额了?
- 三十分钟掌握STL
- 谷歌AI新升级,装备审美功能给照片评分
- 【设计模式】—— 适配器模式Adapter
- 【设计模式】—— 单例模式Singleton
- 【Spring实战】—— 3 使用facotry-method创建单例Bean总结
- JS面向对象高级特性
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- python pyecharts数据可视化 词云图 仪表盘 水球图
- python jupyter notebook配置 更改默认工作目录 更换皮肤主题 代码字体 大小
- 关于直播卖货系统平台在微信浏览器中音视频播放的问题
- python爬虫 scrapy爬虫框架的基本使用
- Python opencv图像处理基础总结(五) 图像金字塔 图像梯度 Canny算法边缘提取
- python scrapy爬虫练习(1) 爬取豆瓣电影top250信息
- python爬虫 senlenium爬取拉勾网招聘数据
- Python opencv图像处理基础总结(六) 直线检测 圆检测 轮廓发现
- 简单又强大的pandas爬虫 利用pandas库的read_html()方法爬取网页表格型数据
- python pyecharts数据可视化 折线图 箱形图
- Python爬虫 selenium自动化 利用搜狗搜索爬取微信公众号文章信息
- python 办公自动化系列 (1) 从22053条数据中统计断网次数并计算平均断网时间
- Python数据可视化 热力图
- python 中文情感分析 Snownlp库的使用
- Python数据分析实战(1) 成都土地市场分析