题干:

The Little Elephant loves sortings.

He has an array a consisting of n integers. Let's number the array elements from 1 to n, then the i-th element will be denoted as ai. The Little Elephant can make one move to choose an arbitrary pair of integers l and r (1 ≤ l ≤ r ≤ n) and increase aiby 1 for all i such that l ≤ i ≤ r.

Help the Little Elephant find the minimum number of moves he needs to convert array a to an arbitrary array sorted in the non-decreasing order. Array a, consisting of nelements, is sorted in the non-decreasing order if for any i (1 ≤ i < nai ≤ ai + 1holds.

Input

The first line contains a single integer n (1 ≤ n ≤ 105) — the size of array a. The next line contains n integers, separated by single spaces — array a (1 ≤ ai ≤ 109). The array elements are listed in the line in the order of their index's increasing.

Output

In a single line print a single integer — the answer to the problem.

Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.

Examples

Input

3
1 2 3

Output

0

Input

3
3 2 1

Output

2

Input

4
7 4 1 47

Output

6

Note

In the first sample the array is already sorted in the non-decreasing order, so the answer is 0.

In the second sample you need to perform two operations: first increase numbers from second to third (after that the array will be: [3, 3, 2]), and second increase only the last element (the array will be: [3, 3, 3]).

In the third sample you should make at least 6 steps. The possible sequence of the operations is: (2; 3), (2; 3), (2; 3), (3; 3), (3; 3), (3; 3). After that the array converts to [7, 7, 7, 47].

题目大意:

给你n个数,定义一次操作:任选一个区间+1,目的是让后一个数不能小于前一个数(不下降子序列),然后最小操作数是多少?

解题报告:

脑补一下过程就好了。就好了。。注意longlong。(好像好多人int交是错的)

首先肯定是从前往后扫,因为这个问题如果倒着找的话,你不知道应该加到多少(因为前面的数字还没有定下来)。从前往后找但是相对差值是不变的,因为每次加的时候肯定要连带着后面的数字一块加,这样是最方便的。想到这里代码就出来了。

AC代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define ll long long
#define pb push_back
#define pm make_pair
#define fi first
#define se second
using namespace std;
const int MAX = 2e5 + 5;
ll a[MAX];
int main()
{int n;ll ans = 0,cur = 0;cin>>n;for(int i = 1; i<=n; i++) scanf("%lld",a+i); cur = a[1];for(int i = 1; i<=n; i++) {if(a[i] < cur) {ans += (cur-a[i]);}cur = a[i];}printf("%lld\n",ans);return 0 ;}

错误代码(找到的一个错误代码):总之这个代码错误很多,比如应该是if(arr[i]<base),再比如else中也应该更新base,,,但是有一个值得注意的点就是那个else中,不能更新minn!!

#include <iostream>
#include <algorithm>
#include <string>
#include <map>
#include <set>
#include <vector>
#include <stack>
#include <queue>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cstring>
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;int main(){int n,i;ll base=0;;cin>>n;ll arr[100010]={0},count=0,mint=0;for(i=0;i<n;i++){cin>>arr[i];if(arr[i]>=base){count+=base-mint;base=arr[i];mint=arr[i];}else{mint=min(mint,arr[i]);}}cout<<count+(base-mint)<<endl;return 0;
}

总结:

其实整个题的思路是,看样例猜算法,猜出个大概的,(或者猜出好多个算法),然后证明一下看哪个是正确的,或者帮助我们排除掉一些错误的。

思维过程是,这题首先想到是不能模拟的啊,1e5的数据量,500ms的时间,只能O(n),所以肯定不能模拟整个过程,然后开始找规律,找到,写代码,提交,AC。

【CodeForces - 205B 】Little Elephant and Sorting (思维)相关推荐

  1. CodeForces 258D Little Elephant and Broken Sorting(期望)

    CF258D Little Elephant and Broken Sorting 题意 题意翻译 有一个\(1\sim n\)的排列,会进行\(m\)次操作,操作为交换\(a,b\).每次操作都有\ ...

  2. CF--思维练习--CodeForces - 221C-H - Little Elephant and Problem (思维)

    ACM思维题训练集合 The Little Elephant has got a problem - somebody has been touching his sorted by non-decr ...

  3. Codeforces Round #459 (Div. 2) C 思维,贪心 D 记忆化dp

    Codeforces Round #459 (Div. 2) C. The Monster 题意:定义正确的括号串,是能够全部匹配的左右括号串. 给出一个字符串,有 (.). ? 三种字符, ? 可以 ...

  4. CF--思维练习--CodeForces - 220C Little Elephant and Shifts (STL模拟)

    ACM思维题训练集合 The Little Elephant has two permutations a and b of length n, consisting of numbers from ...

  5. codeforces 148 C. Terse princess(思维,构造)

    C. Terse princess(思维,构造) 题目链接:codeforces 148C 题意:     有个公主找王子,如果这个人比之前所有王子的财富都高,公主就会Oh,              ...

  6. CodeForces - 1498D Bananas in a Microwave(思维+dp)

    题目链接:点击查看 题目大意:给出 nnn 次操作,初始时有一个 k=0k=0k=0,每次操作抽象为三个数 txyt\ x\ yt x y,其中 xxx 可能为小数,可以选择一个 num∈[0,y]n ...

  7. CodeForces - 813E Army Creation(主席树+思维)

    题目链接:点击查看 题目大意:给出一个长度为 nnn 的数列和一个整数 kkk,现在有 qqq 次询问,每次询问给出一个区间 [l,r][l,r][l,r],设 cnticnt_icnti​ 为数字 ...

  8. CodeForces - 1030C Vasya and Golden Ticket(思维)

    题目链接:点击查看 题目大意:给定长度为n的字符串,字符串全部由0~9的数字组成,要求将字符串划分为连续的子字符串,要求每个子字符串的和都要相等,问给定字符串能否成功划分 题目分析:简单思维,因为划分 ...

  9. CodeForces - 1245A Good ol' Numbers Coloring (思维)

    Codeforces Round #597 (Div. 2 Consider the set of all nonnegative integers: 0,1,2,-. Given two integ ...

最新文章

  1. .axf文件_干货!STM32晶振的更改,BIN文件的生成
  2. Vue中用TypeScript改写JavaScript及装饰器使用
  3. 【安全漏洞】CVE-2021-42287CVE-2021-42278 域内提权
  4. Windows API一日一练(86)GetClipboardData函数
  5. ngx_connection_t结构体
  6. ★教师工资为什么这么低?
  7. notepad设置中文
  8. hibernate脏数据_Hibernate性能提示:脏收集效果
  9. java用scanner类_java中关于scanner类的详细介绍
  10. 买断变订阅!苹果第一付费软件被骂上热搜 官方回应
  11. 北京科技大学计算机专业博导,北京科技大学计算机与通信工程学院-班晓娟
  12. 简单的获取Android手机屏幕的像素
  13. 荐一个不错的UI设计网站: uimaker
  14. 【软件体系结构】软件体系结构概述
  15. 读取阿里云服务器图片到本地
  16. 使用cartopy画飞机的航线
  17. cmake:pkg_check_modules
  18. Lesson 13 'It's only me' 内容赏析
  19. 黑客组织 LAPSUS$ 认领,英伟达超7万员工信息遭泄露时间线
  20. (APIO)烟火表演

热门文章

  1. [Leetcode][第415题][JAVA][字符串相加][双指针]
  2. [Leedcode][JAVA][第14题][最长公共前缀][二分][横竖扫描][分治]
  3. remote addr 取到内网ip_内网穿透工具frp
  4. 裸奔浏览器_躲进浏览器隐私模式就安全了吗?相当于闭着眼睛裸奔
  5. Oracle杀事务数据库崩溃,关于pl/sql dev窗口崩溃导致锁表
  6. python数组元素复制_python的numpy数组 的复制问题?
  7. python调用mysql数据库sql语句过长有问题吗_python连接MYSQL数据库,调用update语句后无法更新数据,解决...
  8. 清空缓存的命令_超详细的mysql数据库查询缓存原理解析、涉及命令、流程分析等...
  9. 16进制字符串转化为10进制数
  10. 在内存中建立文件_磁盘与文件,搞懂它