Codeforces Round #526 (Div. 2)

A. The Fair Nut and Elevator

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

The Fair Nut lives in nn story house. aiai people live on the ii-th floor of the house. Every person uses elevator twice a day: to get from the floor where he/she lives to the ground (first) floor and to get from the first floor to the floor where he/she lives, when he/she comes back home in the evening.

It was decided that elevator, when it is not used, will stay on the xx-th floor, but xx hasn't been chosen yet. When a person needs to get from floor aa to floor bb, elevator follows the simple algorithm:

  • Moves from the xx-th floor (initially it stays on the xx-th floor) to the aa-th and takes the passenger.
  • Moves from the aa-th floor to the bb-th floor and lets out the passenger (if aa equals bb, elevator just opens and closes the doors, but stillcomes to the floor from the xx-th floor).
  • Moves from the bb-th floor back to the xx-th.

The elevator never transposes more than one person and always goes back to the floor xx before transposing a next passenger. The elevator spends one unit of electricity to move between neighboring floors. So moving from the aa-th floor to the bb-th floor requires |a−b||a−b|units of electricity.

Your task is to help Nut to find the minimum number of electricity units, that it would be enough for one day, by choosing an optimal the xx-th floor. Don't forget than elevator initially stays on the xx-th floor.

Input

The first line contains one integer nn (1≤n≤1001≤n≤100) — the number of floors.

The second line contains nn integers a1,a2,…,ana1,a2,…,an (0≤ai≤1000≤ai≤100) — the number of people on each floor.

Output

In a single line, print the answer to the problem — the minimum number of electricity units.

Examples

input

3
0 2 1

output

16

input

2
1 1

output

4

Note

In the first example, the answer can be achieved by choosing the second floor as the xx-th floor. Each person from the second floor (there are two of them) would spend 44 units of electricity per day (22 to get down and 22 to get up), and one person from the third would spend 88units of electricity per day (44 to get down and 44 to get up). 4⋅2+8⋅1=164⋅2+8⋅1=16.

In the second example, the answer can be achieved by choosing the first floor as the xx-th floor.

 

ans=求和求和((i-1)*ai+...(n-1)*an)*4

#include<bits/stdc++.h>
using namespace std;
int main(){
	int n,a,s=0;
	scanf("%d",&n);
	for(int i=0;i<n;i++){
		scanf("%d",&a);
		s+=a*i;
	}
	printf("%d\n",4*s);
} 

B. Kvass and the Fair Nut

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

The Fair Nut likes kvass very much. On his birthday parents presented him nn kegs of kvass. There are vivi liters of kvass in the ii-th keg. Each keg has a lever. You can pour your glass by exactly 11 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by ss liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible.

Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by ss liters of kvass.

Input

The first line contains two integers nn and ss (1≤n≤1031≤n≤103, 1≤s≤10121≤s≤1012) — the number of kegs and glass volume.

The second line contains nn integers v1,v2,…,vnv1,v2,…,vn (1≤vi≤1091≤vi≤109) — the volume of ii-th keg.

Output

If the Fair Nut cannot pour his glass by ss liters of kvass, print −1−1. Otherwise, print a single integer — how much kvass in the least keg can be.

Examples

input

3 3
4 3 5

output

3

input

3 4
5 3 4

output

2

input

3 7
1 2 3

output

-1

Note

In the first example, the answer is 33, the Fair Nut can take 11 liter from the first keg and 22 liters from the third keg. There are 33 liters of kvass in each keg.

In the second example, the answer is 22, the Fair Nut can take 33 liters from the first keg and 11 liter from the second keg.

In the third example, the Fair Nut can't pour his cup by 77 liters, so the answer is −1−1.

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main(){
	ll n,s; 
	ll sum=0,t=2e9,a;
	scanf("%lld%lld",&n,&s);
	for(int i=0;i<n;i++){
		scanf("%lld",&a);
		sum+=a;
		t=min(t,a);
	}
	if(sum<s){
		printf("-1\n");
		return 0;
	}
	if(sum-n*t<s){
		printf("%lld\n",(sum-s)/n);
		return 0;
	}	
	printf("%lld\n",t);
}

C. The Fair Nut and String

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

The Fair Nut found a string ss. The string consists of lowercase Latin letters. The Nut is a curious guy, so he wants to find the number of strictly increasing sequences p1,p2,…,pkp1,p2,…,pk, such that:

  1. For each ii (1≤i≤k1≤i≤k), spi=spi= 'a'.
  2. For each ii (1≤i<k1≤i<k), there is such jj that pi<j<pi+1pi<j<pi+1 and sj=sj= 'b'.

The Nut is upset because he doesn't know how to find the number. Help him.

This number should be calculated modulo 109+7109+7.

Input

The first line contains the string ss (1≤|s|≤1051≤|s|≤105) consisting of lowercase Latin letters.

Output

In a single line print the answer to the problem — the number of such sequences p1,p2,…,pkp1,p2,…,pk modulo 109+7109+7.

Examples

input

abbaa

output

5

input

baaaa

output

4

input

agaa

output

3

Note

In the first example, there are 55 possible sequences. [1][1], [4][4], [5][5], [1,4][1,4], [1,5][1,5].

In the second example, there are 44 possible sequences. [2][2], [3][3], [4][4], [5][5].

In the third example, there are 33 possible sequences. [1][1], [3][3], [4][4].

#include<bits/stdc++.h>
using namespace std;
const int M=1e9+7;	
char s[100005];
int main(){
	int r=0,t=0;
	scanf("%s",s);
	int l=strlen(s);
	
	for(int i=0;i<l;i++){
		if(s[i]=='a'){
			r+=(t+1);
			r%=M;
		}
		else if(s[i]=='b'){
			t=r;
		}
	}
	printf("%d\n",r);
}

D. The Fair Nut and the Best Path

time limit per test

3 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

The Fair Nut is going to travel to the Tree Country, in which there are nn cities. Most of the land of this country is covered by forest. Furthermore, the local road system forms a tree (connected graph without cycles). Nut wants to rent a car in the city uu and go by a simple path to city vv. He hasn't determined the path, so it's time to do it. Note that chosen path can consist of only one vertex.

A filling station is located in every city. Because of strange law, Nut can buy only wiwi liters of gasoline in the ii-th city. We can assume, that he has infinite money. Each road has a length, and as soon as Nut drives through this road, the amount of gasoline decreases by length. Of course, Nut can't choose a path, which consists of roads, where he runs out of gasoline. He can buy gasoline in every visited city, even in the first and the last.

He also wants to find the maximum amount of gasoline that he can have at the end of the path. Help him: count it.

Input

The first line contains a single integer nn (1≤n≤3⋅1051≤n≤3⋅105) — the number of cities.

The second line contains nn integers w1,w2,…,wnw1,w2,…,wn (0≤wi≤1090≤wi≤109) — the maximum amounts of liters of gasoline that Nut can buy in cities.

Each of the next n−1n−1 lines describes road and contains three integers uu, vv, cc (1≤u,v≤n1≤u,v≤n, 1≤c≤1091≤c≤109, u≠vu≠v), where uu and vv — cities that are connected by this road and cc — its length.

It is guaranteed that graph of road connectivity is a tree.

Output

Print one number — the maximum amount of gasoline that he can have at the end of the path.

Examples

input

3
1 3 3
1 2 2
1 3 2

output

3

input

5
6 3 2 5 0
1 2 10
2 3 3
2 4 1
1 5 1

output

7

Note

The optimal way in the first example is 2→1→32→1→3.

The optimal way in the second example is 2→42→4.

 

#include<bits/stdc++.h>
#include<vector>
using namespace std;
typedef long long ll;
vector< pair<ll,ll> > v[300005];
ll val[300005],ans,n;
ll dfs(int u,int fa){
	ll d=val[u];
	ans=max(ans,d);
	for(auto p:v[u] ){
		int to=p.first;
		if(to==fa)
		continue;
		ll t=dfs(to,u);
		ans=max(ans,t+d-p.second);
		d=max(d,val[u]+t-p.second);
	}
	return d;
}
int main(){
	int x,y,z;
	scanf("%lld",&n);
	for(int i=1;i<=n;i++)
	scanf("%lld",&val[i]);
	for(int i=1;i<n;i++){
		scanf("%d%d%d",&x,&y,&z);
		v[x].push_back({y,z});
		v[y].push_back({x,z});
	}
	dfs(1,0);
	printf("%lld\n",ans);
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值