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. Java开发工程师(Web方向) - 03.数据库开发 - 第1章.JDBC

    第1章--JDBC JDBC基础 通过Java Database Connectivity可以实现Java程序对后端数据库的访问 一个完整的数据库部署架构,通常是由客户端和服务器端两部分组成 客户端封 ...

  2. 【swiper】 滑块组件说明

    swiper 滑块视图容器,其原型如下: <swiper indicator-dots="[Boolean]" indicator-color="[Color]&q ...

  3. python程序设计——文件操作

    分类 1.文本文件 存储常规字符串,由若干文本行组成,每行以换行符'\n'结尾 2.二进制文件 把对象以字节串存储,常见的图形图像.可执行文件.数据库文件office文档等 #创建文件 >> ...

  4. 棋盘问题:dfs

    Description 在一个给定形状的棋盘(形状可能是不规则的)上面摆放棋子,棋子没有区别.要求摆放时任意的两个棋子不能放在棋盘中的同一行或者同一列,请编程求解对于给定形状和大小的棋盘,摆放k个棋子 ...

  5. fp-growth树创建代码及详细注释

    事务集过滤重排: #FP树节点结构 class treeNode: def __init__(self,nameValue,numOccur,parentNode): self.name=nameVa ...

  6. 【RL系列】Multi-Armed Bandit笔记——UCB策略与Gradient策略

    本篇主要是为了记录UCB策略与Gradient策略在解决Multi-Armed Bandit问题时的实现方法,涉及理论部分较少,所以请先阅读Reinforcement Learning: An Int ...

  7. nodejs笔记--与Redis的交互篇(六)

    原文地址:http://www.cnblogs.com/zhongweiv/p/node_redis.html 安装前准备 win64: Install python: http://www.pyth ...

  8. 【备忘】mysql常用操作汇总

    1.增删改查 // 插入一条数据 insert into tableName values('liu','bei') // 删除一条数据 delete from tableName where las ...

  9. ptrdiff_t类型

    一.特性 1. 这是一种标准库类型 2. 是两个指针相减的结果的类型(因为差值可能为负值,所以是一种带符号类型) 3. 和size_t一样,ptrdiff_t也是一种定义在<cstddef> ...

  10. Python-列表练习

    1.使用列表生成式生成如下列表:[1,9,25,49,81] s = [i**2 for i in range(1,10)if i%2==1] print(s) 2.输入一个由英文单词组成的字符串(分 ...