Codeforces Round #498 (Div. 3),A,B,C

                           A. Adjacent Replacements

题目链接:http://codeforces.com/contest/1006/problem/A

Mishka got an integer array aa of length nn as a birthday present (what a surprise!).

Mishka doesn't like this present and wants to change it somehow. He has invented an algorithm and called it "Mishka's Adjacent Replacements Algorithm". This algorithm can be represented as a sequence of steps:

  • Replace each occurrence of 11 in the array aa with 22;
  • Replace each occurrence of 22 in the array aa with 11;
  • Replace each occurrence of 33 in the array aa with 44;
  • Replace each occurrence of 44 in the array aa with 33;
  • Replace each occurrence of 55 in the array aa with 66;
  • Replace each occurrence of 66 in the array aa with 55;
  • ……
  • Replace each occurrence of 109−1109−1 in the array aa with 109109;
  • Replace each occurrence of 109109 in the array aa with 109−1109−1.

Note that the dots in the middle of this algorithm mean that Mishka applies these replacements for each pair of adjacent integers (2i−1,2i2i−1,2i) for each i∈{1,2,…,5⋅108}i∈{1,2,…,5⋅108} as described above.

For example, for the array a=[1,2,4,5,10]a=[1,2,4,5,10], the following sequence of arrays represents the algorithm:

[1,2,4,5,10][1,2,4,5,10] →→ (replace all occurrences of 11 with 22) →→ [2,2,4,5,10][2,2,4,5,10] →→ (replace all occurrences of 22 with 11) →→ [1,1,4,5,10][1,1,4,5,10] →→ (replace all occurrences of 33 with 44) →→ [1,1,4,5,10][1,1,4,5,10] →→ (replace all occurrences of 44 with 33) →→ [1,1,3,5,10][1,1,3,5,10] →→ (replace all occurrences of 55 with 66) →→ [1,1,3,6,10][1,1,3,6,10] →→ (replace all occurrences of 66 with 55) →→ [1,1,3,5,10][1,1,3,5,10] →→ …… →→ [1,1,3,5,10][1,1,3,5,10] →→ (replace all occurrences of 1010 with 99) →→ [1,1,3,5,9][1,1,3,5,9]. The later steps of the algorithm do not change the array.

Mishka is very lazy and he doesn't want to apply these changes by himself. But he is very interested in their result. Help him find it.

Input

The first line of the input contains one integer number nn (1≤n≤10001≤n≤1000) — the number of elements in Mishka's birthday present (surprisingly, an array).

The second line of the input contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤1091≤ai≤109) — the elements of the array.

Output

Print nn integers — b1,b2,…,bnb1,b2,…,bn, where bibi is the final value of the ii-th element of the array after applying "Mishka's Adjacent Replacements Algorithm" to the array aa. Note that you cannot change the order of elements in the array.

Examples

input

5
1 2 4 5 10

output

1 1 3 5 9

input

10
10000 10 50605065 1 5 89 5 999999999 60506056 1000000000

output

9999 9 50605065 1 5 89 5 999999999 60506055 999999999

Note

The first example is described in the problem statement.

题目大意:给出一个数组,按照要求输出变换的结果,变换:

所有1变成2,然后所有2变成1;

所有3变成4,然后所有4变成3;

所有5变成6,然后所有6变成5;

.........

hen'很容易就发现规律 了:偶变积不变;

ran'然后直接输出即可:注意范围ll

#include<stdio.h>
#include<string.h>  
#include<math.h>  
  
//#include<map>   
//#include<set>
#include<deque>  
#include<queue>  
#include<stack>  
#include<bitset> 
#include<string>  
#include<iostream>  
#include<algorithm>  
using namespace std;  
  
#define ll long long  
#define INF 0x3f3f3f3f  
#define mod 1000000
#define clean(a,b) memset(a,b,sizeof(a))// ˮӡ 

ll shuzu[1100];

int main()
{
	int n;
	cin>>n;
	for(int i=0;i<n;++i)
	{
		cin>>shuzu[i];
		if(shuzu[i]%2==0)
			shuzu[i]--;
	}
	cout<<shuzu[0];
	for(int i=1;i<n;++i)
		cout<<" "<<shuzu[i];
}

                                       B. Polycarp's Practice

题目链接:http://codeforces.com/contest/1006/problem/B

Polycarp is practicing his problem solving skill. He has a list of nn problems with difficulties a1,a2,…,ana1,a2,…,an, respectively. His plan is to practice for exactly kk days. Each day he has to solve at least one problem from his list. Polycarp solves the problems in the order they are given in his list, he cannot skip any problem from his list. He has to solve all nn problems in exactly kk days.

Thus, each day Polycarp solves a contiguous sequence of (consecutive) problems from the start of the list. He can't skip problems or solve them multiple times. As a result, in kk days he will solve all the nn problems.

The profit of the jj-th day of Polycarp's practice is the maximum among all the difficulties of problems Polycarp solves during the jj-th day (i.e. if he solves problems with indices from ll to rr during a day, then the profit of the day is maxl≤i≤raimaxl≤i≤rai). The total profit of his practice is the sum of the profits over all kk days of his practice.

You want to help Polycarp to get the maximum possible total profit over all valid ways to solve problems. Your task is to distribute all nn problems between kk days satisfying the conditions above in such a way, that the total profit is maximum.

For example, if n=8,k=3n=8,k=3 and a=[5,4,2,6,5,1,9,2]a=[5,4,2,6,5,1,9,2], one of the possible distributions with maximum total profit is: [5,4,2],[6,5],[1,9,2][5,4,2],[6,5],[1,9,2]. Here the total profit equals 5+6+9=205+6+9=20.

Input

The first line of the input contains two integers nn and kk (1≤k≤n≤20001≤k≤n≤2000) — the number of problems and the number of days, respectively.

The second line of the input contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤20001≤ai≤2000) — difficulties of problems in Polycarp's list, in the order they are placed in the list (i.e. in the order Polycarp will solve them).

Output

In the first line of the output print the maximum possible total profit.

In the second line print exactly kk positive integers t1,t2,…,tkt1,t2,…,tk (t1+t2+⋯+tkt1+t2+⋯+tk must equal nn), where tjtj means the number of problems Polycarp will solve during the jj-th day in order to achieve the maximum possible total profit of his practice.

If there are many possible answers, you may print any of them.

Examples

input

8 3
5 4 2 6 5 1 9 2

output

20
3 2 3

input

5 1
1 1 1 1 1

output

1
5

input

4 2
1 2000 2000 2

output

4000
2 2

Note

The first example is described in the problem statement.

In the second example there is only one possible distribution.

In the third example the best answer is to distribute problems in the following way: [1,2000],[2000,2][1,2000],[2000,2]. The total profit of this distribution is 2000+2000=40002000+2000=4000.

题目大意:给出一个长度为n的区间,分成k个,让分成k个的区间中每个区间的最大值之和最大,

第一行:输出输出最大值之和

第二行输出每个区间数的个数;

题目简单,特判一下,然后找数就行了;

#include<stdio.h>
#include<string.h>  
#include<math.h>  
  
//#include<map>   
//#include<set>
#include<deque>  
#include<queue>  
#include<stack>  
#include<bitset> 
#include<string>  
#include<iostream>  
#include<algorithm>  
using namespace std;  

#define ll long long  
#define INF 0x3f3f3f3f  
#define mod 1000000
#define clean(a,b) memset(a,b,sizeof(a))// ˮӡ 

int arr[2100];
int arr2[2100];
int num[2100];
bool cmp(int a,int b)
{
	return a>b;
}

int main()
{
	int n,k;
	cin>>n>>k;
	for(int i=1;i<=n;++i)
	{
		cin>>arr[i];
		arr2[i]=arr[i];
	}
	sort(arr2+1,arr2+n+1,cmp);
	if(k==1)
		cout<<arr2[1]<<endl<<n<<endl;
	else
	{
		int l=0,r=0,max=0;
		for(int i=1;i<=k;++i)
		{
			max=max+arr2[i];
			num[arr2[i]]++;
		}
		int res[2100]={0};
		int can=1;
		for(r=1;r<=n;++r)
		{
			if(num[arr[r]])
			{
			//	cout<<arr[r]<<" "<<r<<" "<<l<<" "<<endl;
				res[can++]=r-l;
				l=r;
				num[arr[r]]--;
			}
		}
		cout<<max<<endl<<res[1];
		for(int i=2;i<k;++i)
			cout<<" "<<res[i];
		//cout<<" "<<res[k];
		cout<<" "<<res[k]+n-l<<endl;
	}
}

                                 C. Three Parts of the Array

题目链接:http://codeforces.com/contest/1006/problem/C

You are given an array d1,d2,…,dnd1,d2,…,dn consisting of nn integer numbers.

Your task is to split this array into three parts (some of which may be empty) in such a way that each element of the array belongs to exactly one of the three parts, and each of the parts forms a consecutive contiguous subsegment (possibly, empty) of the original array.

Let the sum of elements of the first part be sum1sum1, the sum of elements of the second part be sum2sum2 and the sum of elements of the third part be sum3sum3. Among all possible ways to split the array you have to choose a way such that sum1=sum3sum1=sum3 and sum1sum1 is maximum possible.

More formally, if the first part of the array contains aa elements, the second part of the array contains bb elements and the third part contains cc elements, then:

sum1=∑1≤i≤adi,sum1=∑1≤i≤adi,sum2=∑a+1≤i≤a+bdi,sum2=∑a+1≤i≤a+bdi,sum3=∑a+b+1≤i≤a+b+cdi.sum3=∑a+b+1≤i≤a+b+cdi.

The sum of an empty array is 00.

Your task is to find a way to split the array such that sum1=sum3sum1=sum3 and sum1sum1 is maximum possible.

Input

The first line of the input contains one integer nn (1≤n≤2⋅1051≤n≤2⋅105) — the number of elements in the array dd.

The second line of the input contains nn integers d1,d2,…,dnd1,d2,…,dn (1≤di≤1091≤di≤109) — the elements of the array dd.

Output

Print a single integer — the maximum possible value of sum1sum1, considering that the condition sum1=sum3sum1=sum3 must be met.

Obviously, at least one valid way to split the array exists (use a=c=0a=c=0 and b=nb=n).

Examples

input

5
1 3 1 1 4

output

5

input

5
1 3 2 1 4

output

4

input

3
4 1 2

output

0

Note

In the first example there is only one possible splitting which maximizes sum1sum1: [1,3,1],[ ],[1,4][1,3,1],[ ],[1,4].

In the second example the only way to have sum1=4sum1=4 is: [1,3],[2,1],[4][1,3],[2,1],[4].

In the third example there is only one way to split the array: [ ],[4,1,2],[ ][ ],[4,1,2],[ ].

题目大意:给一个数组,分成三个区域,sun1,sum2,sum3;让第一个区域的数值之和sum1等于第三个区域的数值之和sum3;

然后取尽量大的sum1=sum3;

题目也比较水,但是for  for暴力肯定超时,设置两个游标,从头到尾和从尾到头,加上就行了;

注意范围要ll;

#include<stdio.h>
#include<string.h>  
#include<math.h>  
  
//#include<map>   
//#include<set>
#include<deque>  
#include<queue>  
#include<stack>  
#include<bitset> 
#include<string>  
#include<iostream>  
#include<algorithm>  
using namespace std;  

#define ll long long  
#define INF 0x3f3f3f3f  
#define mod 1000000
#define clean(a,b) memset(a,b,sizeof(a))// ˮӡ 

ll arr[200100];
ll arr1[200100];
ll arr2[200100];
int n;


int main()
{
	cin>>n;
	for(int i=1;i<=n;++i)
		cin>>arr[i];
	clean(arr1,0);
	clean(arr2,0);
	for(int i=1,j=n;i<=n,j>0;++i,--j)
	{
		arr1[i]=arr[i]+arr1[i-1];
		arr2[j]=arr[j]+arr2[j+1];
	}
//	for(int i=1;i<=n;++i)
//		cout<<arr1[i]<<" ";
//	cout<<endl;
//	for(int j=1;j<=n;++j)
//		cout<<arr2[j]<<" ";
	int i=1,j=n;
	ll maxnum=0,maxi,maxj;
	if(n==1)
		cout<<"0"<<endl;
	else
	{
		while(i<j)
		{
			if(arr1[i]==arr2[j])
			{
				maxnum=arr1[i];
				++i;
				--j;
			}
			else if(arr1[i]<arr2[j])
				++i;
			else
				--j;
		}
		cout<<maxnum<<endl;
	}
}

猜你喜欢

转载自blog.csdn.net/qq_40482358/article/details/81142667