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 elements ai, 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.

题意:输入n 接下来 输入n个数

n个数初始都为零 现在可以执行两种操作 增加1或者减少1 例如 i个数增加1时 第i+1,i+2..到n 个数 都增加1

执行一次 算一次操作  问最少经过多少次操作 使得这n个数的值为 输入的排列

解答: for循坏遍历一遍  就可以保证操作数最小

比如第i个位置操作几次 只与第i-1位置上的数有关 因为题目规定的操作只对之后的有影响 (注意理解)!!

注意 :当处理第一位的时候 默认之前一位为0

__int64

别乱用abs

做的一手死!!!

#include<bits/stdc++.h>
using namespace std;
__int64 next,a;
__int64 re;
__int64 n;
__int64 ans;
int main()
{
re=0;
scanf("%I64d",&n);
//scanf("%I64d",&a);
re=0;
next=0;
for(int i=0;i<n;i++)
{
scanf("%I64d",&a);
ans=a-next;
next=a;
if(ans<0)
ans=-ans;
re+=ans; }
printf("%I64d\n",re);
return 0;
}

Codeforces Round #331 (Div. 2) B. Wilbur and Array的更多相关文章

  1. Codeforces Round #331 (Div. 2) B. Wilbur and Array 水题

    B. Wilbur and Array Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/596/p ...

  2. Codeforces Round #331 (Div. 2) E. Wilbur and Strings dfs乱搞

    E. Wilbur and Strings Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/596 ...

  3. Codeforces Round #331 (Div. 2) D. Wilbur and Trees 记忆化搜索

    D. Wilbur and Trees Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/596/p ...

  4. Codeforces Round #331 (Div. 2)C. Wilbur and Points 贪心

    C. Wilbur and Points Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/596/ ...

  5. Codeforces Round #331 (Div. 2) A. Wilbur and Swimming Pool 水题

    A. Wilbur and Swimming Pool Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/conte ...

  6. Codeforces Round #331 (Div. 2) C. Wilbur and Points

    C. Wilbur and Points time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  7. Codeforces Round #331 (Div. 2) _A. Wilbur and Swimming Pool

    A. Wilbur and Swimming Pool time limit per test 1 second memory limit per test 256 megabytes input s ...

  8. Codeforces Round #331 (Div. 2)

    水 A - Wilbur and Swimming Pool 自从打完北京区域赛,对矩形有种莫名的恐惧.. #include <bits/stdc++.h> using namespace ...

  9. Codeforces Round #331 (Div. 2) A

    A. Wilbur and Swimming Pool time limit per test 1 second memory limit per test 256 megabytes input s ...

随机推荐

  1. ionic ios样式偏移解决方案。

    在css属性内增加: .item-ios [item-end] { //解决ios系统上尾部图标出现重影而增加的格式. margin: 0px -15.3px 0px 0px; margin-bott ...

  2. Java进阶知识点:并发容器背后的设计理念

    一.背景 容器是Java编程中使用频率很高的组件,但Java默认提供的基本容器(ArrayList,HashMap等)均不是线程安全的.当容器和多线程并发编程相遇时,程序员又该何去何从呢? 通常有两种 ...

  3. Elasticsearch 相同内容文档,不同score(评分)的奇怪问题

    原文:http://stackoverflow.com/questions/14580752/elasticsearch-gives-different-scores-for-same-documen ...

  4. Python3 数值类型与运算符

    1.数值类型与进制 (1)基本类型 整型:int 浮点型:float 布尔类型:bool 复数:complex print(type(1)) print(type(1.1)) print(type(F ...

  5. 团队Beta阶段事后分析

    团队Beta阶段事后分析 设想和目标 我们的软件要解决什么问题?是否定义得很清楚?是否对典型用户和典型场景有清晰的描述? 我们的软件要解决用户的休闲娱乐问题,为用户提供好玩的模拟经营类的游戏,游戏主题 ...

  6. SQL Server 复制:事务发布(读写分离)

    一.背景 在复制的运用场景中,事务发布是使用最为广泛的,我遇到这样一个场景:在YangJiaLeClub数据库中有表.存储过程.视图.用户定义函数,需要提供给其它程序读取放入缓存,程序需要比较及时的获 ...

  7. <Android>tab选项卡

    1.继承TabActivity实现 a)         在布局文件中使用FrameLayout列出Tab组件及Tab中的内容组件 b)        Activity要继承TabActivity c ...

  8. SQL 跨库查询

    使用SQL查询数据,不仅能查询当前库的数据,还可以跨数据库,甚至跨服务器查询. 下面给大家介绍一下跨服务器查询的步骤(以SQL Server为例): 1,建立数据库链接 EXEC sp_addlink ...

  9. lol人物模型提取(二)

      两个dds文件怎么导入到一个模型上呢?这模型又不能拆开.   一开始我想的是用两个材质球来完成,一个材质球对应一个dds文件,然而行不通.   一个材质球对应两个dds文件还不太会弄,于是我想着干 ...

  10. 用svmpredict输出的结果为空

    源程序: