Pizza Separation

CodeForces - 895A 

LiMn2O4有一天在逸夫楼的FZ 131看门,到了中午他订了打卤馕外卖,没想到practer此时要和他抢打卤馕吃,怎么办呢?聪明的你需要想出一个方案,能让上面二位都满意,也就是他们分得的打卤馕大小的差最小

分的时候他们让LiMn2O4先从一块开始取连续的一部分(这些块是挨在一起的),剩下的留给practer

馕已经被切成了n块,每块有一个整数ai表示,ai代表这块打卤馕的在整个打卤馕占的角度, (1 ≤ ai ≤ 360)  。这N块的角度保证值和等于360度

Input

第一行有一个整数 n (1 ≤ n ≤ 360)  代表送过来的打卤馕切成的块数

接下来的一行里有 n个整数 ,每个ai代表这块打卤馕的角度,这n个数的和等于360

Output

输出一个整数,表示Limn2o4和practer分到打卤馕的角度的最小的差的绝对值

Example

Input

4
90 90 90 90

Output

0

Input

3
100 100 160

Output

40

Input

1
360

Output

360

Input

4
170 30 150 10

Output

0

Note

第一个例子里limn2o4可以拿1 和 2 这两块, practer拿 3 和4 . 答案就是 |(90 + 90) - (90 + 90)| = 0.

第三个例子里只有一个人能拿到打卤馕,答案就是|360 - 0| = 360.

在第四个例子里,limn2o4会拿到 1 和 4 ,剩下的给practer,也就是 2 和3 . 答案是 |(170 + 10) - (30 + 150)| = 0.

下面是示意图:

绿色区域和红色区域的面积相等,正两个人会分到相同大小的打卤馕

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
typedef long long ll;
const int INF = 0x3f3f3f3f;
int a[800];
int main(){
	int n,x;
	cin>>n;
	for(int i=1;i<=n;i++){
		cin>>x;
		a[i]=x;
		a[i+n]=x;
	}
	int ans=INF;
	for(int i=1;i<=n;i++){
		int res=0;
		for(int j=i;j<=2*n;j++){
			res+=a[j];
			if(res>=180) break;
		}
		ans=min(ans,res);
	}
	printf("%d\n",abs(360-ans-ans));
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_42759455/article/details/82054564