Codeforces Round #548 (Div. 2)

A. Even Substrings

time limit per test

0.5 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

You are given a string s=s1s2…sns=s1s2…sn of length nn, which only contains digits 11, 22, ..., 99.

A substring s[l…r]s[l…r] of ss is a string slsl+1sl+2…srslsl+1sl+2…sr. A substring s[l…r]s[l…r] of ss is called even if the number represented by it is even.

Find the number of even substrings of ss. Note, that even if some substrings are equal as strings, but have different ll and rr, they are counted as different substrings.

Input

The first line contains an integer nn (1≤n≤650001≤n≤65000) — the length of the string ss.

The second line contains a string ss of length nn. The string ss consists only of digits 11, 22, ..., 99.

Output

Print the number of even substrings of ss.

Examples

input

Copy

4
1234

output

Copy

6

input

Copy

4
2244

output

Copy

10

Note

In the first example, the [l,r][l,r] pairs corresponding to even substrings are:

  • s[1…2]s[1…2]
  • s[2…2]s[2…2]
  • s[1…4]s[1…4]
  • s[2…4]s[2…4]
  • s[3…4]s[3…4]
  • s[4…4]s[4…4]

In the second example, all 1010 substrings of ss are even substrings. Note, that while substrings s[1…1]s[1…1] and s[2…2]s[2…2] both define the substring "2", they are still counted as different substrings.

题意看懂不好说,直接说写法吧。遍历,经过一个数就odd++统计,遇到偶数,当前偶数可以之前的数匹配,所以ans+=odd

#include
using namespace std;
const int N=65000+10;
char s[N];
int n,a[N];
int main()
{
	cin>>n>>s+1;
	for(int i=1;i<=n;i++) a[i]=s[i]-'0';
	int odd=0,ans=0;
	for(int i=1;i<=n;i++)
	{
		odd++;
		if(a[i]%2==0) ans+=odd;
	}
	cout<

B. Chocolates

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

You went to the store, selling nn types of chocolates. There are aiai chocolates of type ii in stock.

You have unlimited amount of cash (so you are not restricted by any prices) and want to buy as many chocolates as possible. However if you buy xixi chocolates of type ii (clearly, 0≤xi≤ai0≤xi≤ai), then for all 1≤j

  • xj=0xj=0 (you bought zero chocolates of type jj)
  • xj

For example, the array x=[0,0,1,2,10]x=[0,0,1,2,10] satisfies the requirement above (assuming that all ai≥xiai≥xi), while arrays x=[0,1,0]x=[0,1,0], x=[5,5]x=[5,5] and x=[3,2]x=[3,2] don't.

Calculate the maximum number of chocolates you can buy.

Input

The first line contains an integer nn (1≤n≤2⋅1051≤n≤2⋅105), denoting the number of types of chocolate.

The next line contains nn integers aiai (1≤ai≤1091≤ai≤109), denoting the number of chocolates of each type.

Output

Print the maximum number of chocolates you can buy.

Examples

input

Copy

5
1 2 1 3 6

output

Copy

10

input

Copy

5
3 2 5 4 10

output

Copy

20

input

Copy

4
1 1 1 1

output

Copy

1

Note

In the first example, it is optimal to buy: 0+0+1+3+60+0+1+3+6 chocolates.

In the second example, it is optimal to buy: 1+2+3+4+101+2+3+4+10 chocolates.

In the third example, it is optimal to buy: 0+0+0+10+0+0+1 chocolates.

就是构造 从后依次递减的最大数即可

#include
using namespace std;
const int N=2e5+10;
typedef long long ll;
ll a[N];
int n;
int main()
{
	cin>>n;
	for(int i=1;i<=n;i++)
	{
		scanf("%lld",&a[i]);
	}
	ll ans=0;
	for(int i=n;i>=1;i--)
	{
		if(i==n)
		{
			ans+=a[i];
			continue;
		}
		if(a[i]>=a[i+1])
		{
			a[i]=a[i+1]-1;
		}
		if(a[i]<=0) a[i]=0;
		ans+=a[i];
	}
	cout<

C. Edgy Trees

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

You are given a tree (a connected undirected graph without cycles) of nn vertices. Each of the n−1n−1 edges of the tree is colored in either black or red.

You are also given an integer kk. Consider sequences of kk vertices. Let's call a sequence [a1,a2,…,ak][a1,a2,…,ak] good if it satisfies the following criterion:

  • We will walk a path (possibly visiting same edge/vertex multiple times) on the tree, starting from a1a1 and ending at akak.
  • Start at a1a1, then go to a2a2 using the shortest path between a1a1 and a2a2, then go to a3a3 in a similar way, and so on, until you travel the shortest path between ak−1ak−1 and akak.
  • If you walked over at least one black edge during this process, then the sequence is good.

Codeforces Round #548 (Div. 2)_第1张图片

Consider the tree on the picture. If k=3k=3 then the following sequences are good: [1,4,7][1,4,7], [5,5,3][5,5,3] and [2,3,7][2,3,7]. The following sequences are not good: [1,4,6][1,4,6], [5,5,5][5,5,5], [3,7,3][3,7,3].

There are nknk sequences of vertices, count how many of them are good. Since this number can be quite large, print it modulo 109+7109+7.

Input

The first line contains two integers nn and kk (2≤n≤1052≤n≤105, 2≤k≤1002≤k≤100), the size of the tree and the length of the vertex sequence.

Each of the next n−1n−1 lines contains three integers uiui, vivi and xixi (1≤ui,vi≤n1≤ui,vi≤n, xi∈{0,1}xi∈{0,1}), where uiui and vivi denote the endpoints of the corresponding edge and xixi is the color of this edge (00 denotes red edge and 11 denotes black edge).

Output

Print the number of good sequences modulo 109+7109+7.

Examples

input

Copy

4 4
1 2 1
2 3 1
3 4 1

output

Copy

252

input

Copy

4 6
1 2 0
1 3 0
1 4 0

output

Copy

0

input

Copy

3 5
1 2 1
2 3 0

output

Copy

210

Note

In the first example, all sequences (4444) of length 44 except the following are good:

  • [1,1,1,1][1,1,1,1]
  • [2,2,2,2][2,2,2,2]
  • [3,3,3,3][3,3,3,3]
  • [4,4,4,4][4,4,4,4]

In the second example, all edges are red, hence there aren't any good sequences.

这个题真的有意思。不管给出多少点,能构成路的所有情况就是n^k个。刚开始想叉了,想着把黑边的某个联通块点数m求出来,然后答案是m^k,但是该联通块还可以与红联通块组成路径,然后就一直卡着不知道怎么写了,耗费了近一个小时发现,可以反过来做,把红边的某个联通块点数m求出来,然后答案不就是n^k-m^k-剩下的红点

#include
using namespace std;
const int N=1e5+10;
typedef long long ll;
const ll mod=1e9+7;
ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); 
for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}
 
ll n,k;
int u,v,w;
vectorG[N],val[N];
int  vis[N];

ll num;
void dfs(int id,ll &num)
{
	int siz=G[id].size();
	for(int i=0;i>n>>k;
	for(int i=1;i

 

你可能感兴趣的:(codeforce题解)