给出原始{ai}={0},{bi}。每次修改{ai..n}+1或-1,求最小操作次数使{ai}=={bi}。

累计相邻两数差即可。

因为差最大10^9,数字有2*10^5,要long long。

#include <cstdio>
#include <cstdlib>
typedef long long ll;
int main() {static ll a[200001];int n,i;scanf("%d", &n);for(i=1;i<=n;i++) scanf("%I64d", &a[i]);ll ans=abs(a[1]);for(i=2;i<=n;i++)ans+=abs(a[i]-a[i-1]);printf("%I64d", ans);return 0;
}
B. Wilbur and Array
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Wilbur the pig is tinkering with arrays again. He has the array a1, a2, ..., an initially consisting of n zeros. At one step, he can choose any index i and either add 1 to all elementsai, ai + 1, ... , an or subtract 1 from all elements ai, ai + 1, ..., an. His goal is to end up with the array b1, b2, ..., bn.

Of course, Wilbur wants to achieve this goal in the minimum number of steps and asks you to compute this value.

Input

The first line of the input contains a single integer n (1 ≤ n ≤ 200 000) — the length of the array ai. Initially ai = 0 for every position i, so this array is not given in the input.

The second line of the input contains n integers b1, b2, ..., bn ( - 109 ≤ bi ≤ 109).

Output

Print the minimum number of steps that Wilbur needs to make in order to achieve ai = bi for all i.

Sample test(s)
input
5
1 2 3 4 5

output
5

input
4
1 2 2 1

output
3

Note

In the first sample, Wilbur may successively choose indices 1, 2, 3, 4, and 5, and add 1 to corresponding suffixes.

In the second sample, Wilbur first chooses indices 1 and 2 and adds 1 to corresponding suffixes, then he chooses index 4 and subtract 1.

CodeForces 596B Wilbur and Array 贪心相关推荐

  1. CodeForces 596B Wilbur and Array

    简单题,一个一个操作,最后就是答案. #include<cstdio> #include<cstring> #include<cmath> #include< ...

  2. Wilbur and Array

    Wilbur and Array Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u Submit St ...

  3. codeforces Gym 100338E Numbers (贪心,实现)

    题目:http://codeforces.com/gym/100338/attachments 贪心,每次枚举10的i次幂,除k后取余数r在用k-r补在10的幂上作为候选答案. #include< ...

  4. CodeForces 1514A Perfectly Imperfect Array

    CodeForces 1514A Perfectly Imperfect Array 题意: 给你n个数,是否存在一个数不是平方数 题解: 先开方,转int,判断是否等于平方 代码: #include ...

  5. cf596B. Wilbur and Array

    B. Wilbur and Array time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  6. CodeForces - 1480D1 Painting the Array I(贪心)

    题目链接:点击查看 题目大意:给出一个长度为 nnn 的序列,现在要求拆分成两个子序列,使得两个子序列的贡献之和最 大.对于一个序列的贡献就是,去掉相邻且相同的字母后的长度,即 ∑i=1n[a[i]! ...

  7. CodeForces 596 B Wilbur and Array

    <span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255) ...

  8. Codeforces G. Nick and Array(贪心)

    题目描述: Nick had received an awesome array of integers a=[a1,a2,-,an] as a gift for his 5 birthday fro ...

  9. CodeForces - 1480D2 Painting the Array II(dp)

    题目链接:点击查看 题目大意:给出一个长度为 nnn 的序列,现在要求拆分成两个子序列,使得两个子序列的贡献之和最 小.对于一个序列的贡献就是,去掉相邻且相同的字母后的长度,即 ∑i=1n[a[i]! ...

最新文章

  1. 2022-2028年中国低氧铜杆行业市场研究及前瞻分析报告
  2. Copy GAC DLL
  3. 【Linux】一步一步学Linux——useradd命令(82)
  4. wordpress去掉自带的logo或者左侧栏的菜单方法
  5. php七牛持久化处理,使用七牛自定义数据处理范例
  6. oracle数据库赋权_(转)Oracle数据库如何授权收费(Database Licensing)
  7. 修改配置文件,编译freeswitch支持H264
  8. 【生信进阶练习1000days】day7-RSQLite的使用
  9. KindEditor
  10. 鬼谷八荒逆天改命存档
  11. 滤波电容/去耦电容,怎么选型?
  12. Github 上 lux 下载神器的安装及使用教程
  13. 记录一次紫狐Rootkit应急响应过程
  14. 23个开源App的App Store地址和源代码
  15. JavaWeb - Request Response
  16. 学生成绩平均绩点计算:绩点计算器(5.0分制,Java、C实现)
  17. 易得无价宝,难得有情郎
  18. 六大设计原则 (SOLID)
  19. 企业如何使用商业智能(BI)进行数据赋能?
  20. 计算机基础课程的金课,我校8门课程获批省级线上“金课”

热门文章

  1. 数据库的一些基本语法一
  2. Vivado 自定义暗黑色主题字体、颜色,复用自定义主题。文末附黑色主题设置文件
  3. Linux网络服务——FTP服务器搭建
  4. ssh整合:spring+springmvc+hibernate
  5. android课程表
  6. 是时候使用Markdown写作了
  7. 游戏代理加盟需要什么条件?
  8. 如何有效地帮助新人融入项目中
  9. 欧美风相册PPT模板
  10. 网易云音乐APP(基于APICloud平台)