We have an integer sequence of length NN: x=(x_0,x_1,\cdots,x_{N-1})x=(x0​,x1​,⋯,xN−1​) (note that its index is 00-based). Initially, all elements of xx are 00.

You can repeat the following operation any number of times.

  • Choose integers i,ki,k (0 \leq i \leq N-10≤i≤N−1, 1 \leq k \leq N1≤k≤N). Then, for every jj such that i \leq j \leq i+k-1i≤j≤i+k−1, increase the value of x_{j\bmod N}xjmodN​ by 11.

You are given an integer sequence of length NN: A=(A_0,A_1,\cdots,A_{N-1})A=(A0​,A1​,⋯,AN−1​). Find the minimum number of operations needed to make xx equal AA.

Constraints

  • 1 \leq N \leq 2000001≤N≤200000
  • 1 \leq A_i \leq 10^91≤Ai​≤109
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

NN
A_0A0​ A_1A1​ \cdots⋯ A_{N-1}AN−1​

Output

Print the answer.

Sample 1

Inputcopy Outputcopy
4
1 2 1 2
2

We should do the following.

  • Initially, we have x=(0,0,0,0)x=(0,0,0,0).
  • Do the operation with i=1,k=3i=1,k=3, making x=(0,1,1,1)x=(0,1,1,1).
  • Do the operation with i=3,k=3i=3,k=3, making x=(1,2,1,2)x=(1,2,1,2).

Sample 2

Inputcopy Outputcopy
5
3 1 4 1 5
7

Sample 3

Inputcopy Outputcopy
1
1000000000
1000000000

思路:我们需要把数组从0变成他给出的数组,不妨考虑把他给的数组变成全是0,因为全是0,原数组的数相等,所以差分数组最后全是0,我们要把他的差分变成0,他是一个环(因为j超过n-1就会modn),所以差分数组的正数和等于负数和,因为我们本来需要对原数组进行+1的操作,但是我们现在转化思维对他给出的数组进行-1的操作使他变成0,当我们对他l-r的区间进行减1的操作时差分数组就会进行一个b[l]-1,b[r+1]+1的操作,因为lr是我们任意选的,所以我们只用操作差分的正数之和个操作就可以让原数组一样(选一个正数-1,选一个负数+1),但是当最小的值太大的时候我们把差分数组变成0之后只是让他们变成了一样的数,还需要把操作之后的n个相同的数x操作x下减到0,根据贪心的思想我们每次操作势必会对max(a[i])操作,假设操作了o次,操作之后的数再变成0所需要的操作数h加上o肯定还是max(a[i]),我们把max(a[i])和把差分数组变成0所进行的con次操作数做比较,取最大值:如果max(a[i])>con说明他经过con次操作只是把原数组变成了一样的数而没有变成零,所以总共需要进行max(a[i])次操作,如果con>max(a[i])说明经过con次操作不仅原数组的数一样而且等于0。

注意:以后不管是整数多大都给我整成longlong!!!int不过!!!!

#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;long long a[200000];int main(){long long n,ans=0;scanf("%lld",&n);for(long long i=0;i<n;i++){scanf("%lld",&a[i]);}long long max1=a[0];for(long long  i=1;i<n;i++){long long t=a[i]-a[i-1];if(t>0){ans+=t;}if(a[i]>max1){max1=a[i];}}long long q=a[0]-a[n-1];if(q>0){ans+=q;}ans=max(max1,ans);printf("%lld",ans);return 0;
}

D - Circular Addition相关推荐

  1. [ASP.NET4之旅]Circular file references are not allowed

    将ASP.NET 2.0的项目升级到ASP.NET 4后,用VS2010编译站点,某些控件出现编译错误"Circular file references are not allowed&qu ...

  2. 【UVA/Codeforces】1584 Circular Sequence / 792B Counting-out Rhyme(就是一个圈儿...)

    https://vjudge.net/problem/UVA-1584 1584 Circular Sequence 输入一个字符串,可以以字符串中任意一个字母作为起始,输出字典序最小的那个字符串 两 ...

  3. python使用matplotlib可视化饼图(pie plot)、可视化嵌套的环形饼图(Nested circular pie chart)

    python使用matplotlib可视化饼图(pie plot).可视化嵌套的环形饼图(Nested circular pie chart) 目录 python使用matplotlib

  4. partially initialized module ‘numpy‘ has no attribute ‘array‘ (most likely due to a circular import)

    partially initialized module 'numpy' has no attribute 'array' (most likely due to a circular import) ...

  5. Accurate circular consensus long-read sequencing improves variant detection and assembly of a human

    Accurate circular consensus long-read sequencing improves variant detection and assembly of a human ...

  6. Requested bean is currently in creation: Is there an unresolvable circular reference?

    2019独角兽企业重金招聘Python工程师标准>>> 今天开发写代码,运行代码的时候 出现了 这个错误 : Requested bean is currently in creat ...

  7. Linux平台Qt creator报错:Circular all - first dependency dropped

    在Linux下安装好Qt 5.0之后,使用Qt Creator创建了一个基于QMainWindow的框架程序.原本应该可以顺利的完成编译工作,因为自带的模板工程没有经过任何修改.可是在编译整个工程的时 ...

  8. WEEX 报错 TypeError: Converting circular structor to JSON 的解决方法

    背景 在进行千牛QAP插件开发的时候,有时会遇到 TypeError: Converting circular structor to JSON,并且看不到报错位置,对新手可能会造成疑惑. 原因 we ...

  9. python 报错 most likely due to a circular import 解决方法

    原因各个python文件,互相引用,造成的 循环引用问题. 解决方法:把需要引用的独立成一个文件,让其单向引用 使用python写一个稍微大一点的工程时,经常会遇到循环import,即cicular ...

最新文章

  1. Oracle嵌套表实例说明
  2. PAT甲级1144 The Missing Number:[C++题解]哈希表
  3. 卸载vuecli3_卸载vue2.0并升级vue_cli3.0的实例讲解
  4. gitkraken把github上的东西clone到本地
  5. CDH6.x Solr7.x 集成 Ik 分词
  6. php callable 参数,php 利用反射执行callable
  7. 手机号正则_一起刷题学习正则表达式
  8. Excel 使用技巧之 -- 统计、过滤与引用数据
  9. 期货品种产业链图表_工业
  10. webstorm 高效开发 (html)
  11. 记Python的一些用法
  12. 信息安全工程师 学习笔记 完结
  13. Java面试必看的18个开源项目
  14. NVIDIA GeForce Experience报错:“验证程序加载失败,请检查您的浏览器设置,例如广告拦截程序”的解决方法
  15. [译]PostgreSQL15 public shema权限增强
  16. hdu1728 (直撞bfs)
  17. ElasticSearch 2 (25) - 语言处理系列之同义词
  18. C语言汇总学生成绩,c语言用二维数组统计学生成绩.doc
  19. 看完南京前11年房价,我哭了!今年实在太特么狠了
  20. 从源代码分析DbSet如何通过ObjectStateManager管理entity lifecycle的生命周期

热门文章

  1. 蓝牙耳机哪款好用?这些选购小技巧帮你选到更适合你的蓝牙耳机!
  2. D3.js-基础知识
  3. 受约束的 Delaunay 2D
  4. 获取具体日期N个月前(后)的自然月时间戳
  5. php 访问服务器上图片不显示,php显示云服务器上图片不显示
  6. 如何在Windows中使用Socks5代理IP保障网络安全
  7. 查找2-n之间素数的个数
  8. 管理的基础知识都包含哪些内容
  9. 算法工程师与软件开发工程师的区别
  10. Linux的firewalld防火墙学习笔记220929