C. Money Transfers
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

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 to0.

Output

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

Examples
input
5 0 -5
output

input
-1 0 1 0
output

input
1 2 3 -6
output

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 then1 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.
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <algorithm>
#include <set>
using namespace std;
typedef long long ll;
typedef unsigned long long Ull;
#define MM(a,b) memset(a,b,sizeof(a));
const double eps = 1e-10;
const int inf =0x7f7f7f7f;
const double pi=acos(-1);
const int maxn=100000; int a[maxn+10];
map<ll,int> mp;
int main()
{
int n;
while(~scanf("%d",&n))
{
mp.clear();
for(int i=1;i<=n;i++)
scanf("%d",&a[i]); int ans=n-1;ll sum=0;
for(int i=1;i<=n;i++)
{
sum+=a[i];
mp[sum]++;
ans=min(ans,n-1-(mp[sum]-1));
}
printf("%d\n",ans);
}
return 0;
}

  智商被碾压了,,,,,呜呜呜,,看看这篇题解基本就会了,,,让我再哭会儿

http://blog.csdn.net/acm_fighting/article/details/51437452

最新文章

  1. Call to undefined function mysql_connnect()
  2. angularJs自定义指令.directive==类似自定义标签
  3. 【leetcode】368. Largest Divisible Subset
  4. Elasticsearch .Net Client NEST 索引DataSet数据
  5. SpringMVC学习系列- 表单验证
  6. Windows Phone 8初学者开发—第23部分:测试并向应用商店提交
  7. Mac 下安装配置Mysql
  8. SpringMVC:学习笔记(1)——理解MVC及快速入门
  9. Python爬虫从入门到放弃(十八)之 Scrapy爬取所有知乎用户信息(上)
  10. 201521123063 《Java程序设计》第13周学习总结
  11. 批量ping测试的脚本
  12. Asp.Net Core微信服务中间件-.NetCore2.1
  13. 3.HTML+CSS 制作个太阳
  14. Windows 获取unix timestamp
  15. Spring Boot -05- 多模块结构项目构建与测试(详细图文教程)IDEA 版
  16. POJ 2976 Dropping tests(分数规划)
  17. idea插件Lombok
  18. Vue入坑教程(一)——搭建vue-cli脚手架
  19. JS数据交互:动态从数据库中获取数据填充Select
  20. 研究ecmall一些流程、结构笔记 (转)

热门文章

  1. (转)SQLServer查询数据库各种历史记录
  2. 批量删除Maven本地仓库中未下载完成的jar包(不完整的jar包)
  3. 爬虫实例学习——爬取酷狗TOP500数据
  4. JS数据结构的栈和队列操作
  5. 关于redis的几件小事(九)redis的并发竞争问题
  6. jquery ready load
  7. electron builder 打包多个第三方依赖的软件
  8. 跑满带宽的一款百度网盘下载工具 : PanDownload
  9. 第十篇.3、ython并发编程之多线程理论部分
  10. 借助Charles来测试移动端-上篇