There are n banks in the city where Vasya lives, they are located in a circle, such that any two banks are neighbouring if their indices differ by no more than 1. Also, bank 1 and bank n are neighbours if n > 1. No bank is a neighbour of itself.

Vasya has an account in each bank. Its balance may be negative, meaning Vasya owes some money to this bank.

There is only one type of operations available: transfer some amount of money from any bank to account in any neighbouring bank. There are no restrictions on the size of the sum being transferred or balance requirements to perform this operation.

Vasya doesn't like to deal with large numbers, so he asks you to determine the minimum number of operations required to change the balance of each bank account to zero. It's guaranteed, that this is possible to achieve, that is, the total balance of Vasya in all banks is equal to zero.

Input

The first line of the input contains a single integer n (1 ≤ n ≤ 100 000) — the number of banks.

The second line contains n integers ai ( - 109 ≤ ai ≤ 109), the i-th of them is equal to the initial balance of the account in the i-th bank. It's guaranteed that the sum of all ai is equal to 0.

Output

Print the minimum number of operations required to change balance in each bank to zero.

Examples
input
35 0 -5

output
1

input
4-1 0 1 0

output
2

input
41 2 3 -6

output
3

Note

In the first sample, Vasya may transfer 5 from the first bank to the third.

In the second sample, Vasya may first transfer 1 from the third bank to the second, and then 1 from the second to the first.

In the third sample, the following sequence provides the optimal answer:

  1. transfer 1 from the first bank to the second bank;
  2. transfer 3 from the second bank to the third;
  3. transfer 6 from the third bank to the fourth.

 题意:

  n个负担着不同债务的银行围成环,每次每个银行只能向自己左边或者右边的银行转移资金。所有银行的债务和为0,问你最少转移多少次能让所有银行债务都为0。

 题解:

  求前缀和为0的子区间的个数k,区间长度n减去个数k就是答案了。

#include<iostream>
#include<map>
#include<algorithm>
using namespace std;
int main()
{int n;while(cin>>n){map<long long,int> d;long long sum=0;int ans=n-1;for(int i=0;i<n;i++){int x;cin>>x;sum+=x;d[sum]++;ans=min(ans,n-d[sum]);}cout<<ans<<endl;}return 0;
}

转载于:https://www.cnblogs.com/orion7/p/7338045.html

Codeforces 675C Money Transfers (思维题)相关推荐

  1. CF--思维练习-- CodeForces - 215C - Crosses(思维题)

    ACM思维题训练集合 There is a board with a grid consisting of n rows and m columns, the rows are numbered fr ...

  2. CodeForces - 298B Sail (思维题)

    CodeForces - 298B Sail The polar bears are going fishing. They plan to sail from (sx, sy) to (ex, ey ...

  3. ACM思维题训练 Section A

    题目地址: 选题为入门的Codeforce div2/div1的C题和D题. 题解: A:CF思维联系–CodeForces -214C (拓扑排序+思维+贪心) B:CF–思维练习-- CodeFo ...

  4. Codeforces 1077B Disturbed People(思维题)

    Codeforces 1077B Disturbed People(思维题) There is a house with nn flats situated on the main street of ...

  5. codeforces 有意思的思维题 1 ~ 15

    codeforces 思维题 1.给定数组,求满足i < j and ai * aj = i + j的数对数量 2.第 i 步向前跳 i 步或后退 1 步 3.给两个点,求正方形的另两个点 4. ...

  6. CodeForces - 1102A(思维题)

    https://vjudge.net/problem/2135388/origin Describe You are given an integer sequence 1,2,-,n. You ha ...

  7. ☆【CodeForces - 764C】Timofey and a tree (思维题,树的性质)

    题干: Each New Year Timofey and his friends cut down a tree of n vertices and bring it home. After tha ...

  8. Educational Codeforces Round 67 (Rated for Div. 2)(D思维题 线段树/E树形dp(换根dp) 二次扫描与换根法)

    心得 D写了个假算法被hack了wtcl- E据涛神说是二次扫描与换根法,看了看好像和树形dp差不多 F概率dp G费用流 回头再补 思路来源 马老师 归神 贤神等代码 http://www.mami ...

  9. little w and Soda(思维题)

    链接:https://ac.nowcoder.com/acm/contest/297/A 来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 262144K,其他语言5242 ...

最新文章

  1. python中的列表生成式_Python中的列表生成式
  2. 用ASP.net判断上传文件类型的三种方法
  3. Python 推导,内含,迭代器
  4. sed 第n行后加入_【高新课堂】第一百三十九期Liunx运维17个实用技巧
  5. AAA验证和ciscorescue v4.2 验证服务器的搭建(telnet方式和级别的设置)
  6. xml和html是兄弟还是父子?
  7. 【Keil】Keil5无法更改背景色和字体解决方案
  8. 普中科技51单片机_【笔记】普中科技-51单片机-静态数码管显示
  9. 对Linux网络配置管理的简单使用经验
  10. 报告:2020年NFT总市值达5200万美元
  11. 腾讯专利仅次谷歌;​苹果或将 iPhone 订单转给和硕;​Uber 接受比特币支付 | 极客头条...
  12. python中时间的加n和减n运算
  13. 一个卡片式的ViewPager,带你玩转ViewPager的PageTransformer属性!
  14. 实验7 OpenGL光照
  15. HTML5 音频 / 视频 DOM 操作
  16. 水滴状的自定义视图,让您摆脱单调的Dialog
  17. CSAPP第七章家庭作业参考答案
  18. 成长的思考:如何在工作中保持高速的自我成长
  19. uni-app 基础之常用组件(2)基础内容
  20. 芯动联科在科创板IPO过会:拟募资10亿元,金晓冬为实际控制人

热门文章

  1. linux 虚函数调用性能,C++虚函数和多态
  2. mysql外键约束视频教学_外键约束案例_MySQL数据库 快速入门 基础+实战 视频教程_MySQL视频-51CTO学院...
  3. mybatis Android,mybatis使用selectByPrimaryKey出错
  4. 下列哪个可以选中矩形文字块_AI制作字母块文字效果
  5. python 静态方法 类方法 的作用_Python实例方法、类方法、静态方法的区别与作用详解...
  6. 我的世界java版幻翼_我的世界:熬夜3天能见到“幻翼”?你错了,还要满足这7个条件!...
  7. redhad yum 安装mysql_yum安装Mysql
  8. 图像修复中的方法--AI智能.
  9. 声音定位的距离误差到底有多高?
  10. 高频信号对LM386直流偏置的影响