题干:

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

3
5 0 -5

Output

1

Input

4
-1 0 1 0

Output

2

Input

4
1 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为分界线,后来发现好像不太行。参考题解

当有一小段(长度为len)的总和为0时, 我们可以用len-1步来使这一小段使这一小段全部变成0,

考虑将n个银行分成k小段, 由于是环型结构并且总和为0, 不会存在最后凑不成总和为0的一小段的情况.

k小段的长度分别是len1,len2,.....lenk, 所需要的步数分别是len1-1,len2-1,....lenk-1,  len1+len2+...+lenk

是等于n的, 然后k个-1, 最后就是n-k, 要使n-k最小, k要尽量大. 就是要把n个数分成尽量多的段数.

考虑前缀和, 统计不同前缀和的个数, 相同的前缀和之间夹的一小段数肯定是总和为0的, 用不同的前缀和

划分这n个数k可能不同, 我们统计这些前缀和并且取最大的.

AC代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<stack>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define FF first
#define SS second
#define ll long long
#define pb push_back
#define pm make_pair
using namespace std;
typedef pair<int,int> PII;
const int MAX = 2e5 + 5;
map<ll,ll> mp;
int n;
ll x,ans,sum;
int main()
{cin>>n;for(int i = 1; i<=n; i++) {cin>>x;sum += x;mp[sum]++;ans = max(ans,mp[sum]);}cout << n-ans << endl;return 0 ;
}

【CodeForces - 675C】Money Transfers(思维,前缀和)相关推荐

  1. Codeforces 675C Money Transfers (思维题)

    There are n banks in the city where Vasya lives, they are located in a circle, such that any two ban ...

  2. Codeforces Round #632 (Div. 2) C. Eugene and an array 思维 + 前缀和

    传送门 文章目录 题意: 思路: 题意: 给定一个长度为nnn的序列aaa,定义一段区间为好区间是这段区间的所有连续子区间的和都不为000,求好区间的个数. 思路: 套路题,定义aia_iai​的前缀 ...

  3. Balanced Substring CodeForces - 873B (思维+前缀和)

    Balanced Substring CodeForces - 873B You are given a string s consisting only of characters 0 and 1. ...

  4. CodeForces-1016C Vasya And The Mushrooms(模拟+思维+前缀和的前缀和) 解题报告 Apare_xzc

    CodeForces-1016C Vasya And The Mushrooms(模拟+思维+二重前缀和 ) 解题报告 xzc 2019/4/7 这周周赛的C题:wyt学姐的恶意   这道题周赛的时候 ...

  5. CodeForces - 1535C Unstable String(思维)

    题目链接:点击查看 题目大意:规定一个字符串将问号都替换成 000 或 111 后满足 010101 交替的话,该字符串是合法的,现在给出一个长度为 nnn 的字符串,求合法子串的个数 题目分析:两种 ...

  6. C. k-Amazing Numbers(思维前缀最小值+枚举相同数距离)

    https://codeforces.com/contest/1417/problem/C 大晚上的想岔了.. a1,a2,-,an (1≤ai≤n)这个范围其实就在暗示要枚举出现的数.但是我不知道怎 ...

  7. 126. 最大的和【思维 前缀和】

    很容易的想到,用二维前缀和,暴力的4层for枚举左上角和右下角的下标. 这样肯定会超时. 我们不妨先考虑一维的情况,一个数组,如何求最大的矩形. 这是一个很简单的DP f[i]=max(f[i-1], ...

  8. CodeForces - 1593G Changing Brackets(思维)

    题目链接:点击查看 题目大意:给出一个长度为 nnn 的括号序列,其中包含了 {(,),[,]}\{(,),[,]\}{(,),[,]} 四种括号,现在可以进行两种操作: 将括号反转,代价为 000, ...

  9. CodeForces - 1567C Carrying Conundrum(思维/状压)

    题目链接:点击查看 题目大意:规定加法中使用隔项进位,问给定的 nnn 有多少种方案可以通过 "隔项进位加法" 得到 题目分析:隔项进位意味着奇偶位置的数字互不影响,所以将奇偶位置 ...

最新文章

  1. 基于深度学习识别模型的缺陷检测
  2. Canvas 波形图
  3. mysql命令:为mysql命令指定字符集
  4. 算法入门篇七 前缀树
  5. 112_Power Pivot 销售订单按 sku 订单类型特殊分类及占比相关
  6. Tomcat日志打印乱码解决方法
  7. 15款Cocos2d-x游戏源码 1
  8. python编程可以自学么-python编程还能自学?怎么能学好? - 【大连东软睿道】
  9. Cocos2d-x-使用脚本概述
  10. Elasticsearch 6.X xpack安装使用详解(试用)
  11. Windows系统连接蓝牙音箱,已连接,没有声音
  12. CIKM 2019 挑战杯「用户行为预测」冠军方案:层次GNN模型在推荐中的应用
  13. bat——批量删除文件文件夹
  14. AutoSAR入门到精通讲解 (AuroSAR-CP描述) 1.1 AutoSAR-CP简介
  15. 总结一下自己的linux历程
  16. linux复制后权限变化,学霸Linux基础命令吐血总结,给你当新华字典用
  17. 智商和情商哪个更重要
  18. [译] 3.泛型和子类型
  19. SylixOS 共用中断号机制
  20. 超级好用的芯片封装网站IC Search

热门文章

  1. hashCode()方法的作用使用分析
  2. XML Schema ---complexType-----复合元素
  3. linux 视频学习
  4. window.open使用中遇到的问题
  5. Access 时间比较错误
  6. C#连接数据库SQL(2005)
  7. python自动化框架测试实操_自动化框架之 python+selenium+pytest
  8. php对话框制作,织梦系统“提示窗口对话框类”详解,oxwindow.class.php、catalog_do.php...
  9. mysql两个表查询修改_MySQL:查询、修改(二)
  10. JS循环执行函数setInterval