Codeforces Round #513(Div. 1 + Div. 2) A B C D题解

传送门

A. Phone Numbers

time limit per test

2 seconds

memory limit per test

512 megabytes

input

standard input

output

standard output

Let's call a string a phone number if it has length 11 and fits the pattern "8xxxxxxxxxx", where each "x" is replaced by a digit.

For example, "80123456789" and "80000000000" are phone numbers, while "8012345678" and "79000000000" are not.

You have nn cards with digits, and you want to use them to make as many phone numbers as possible. Each card must be used in at most one phone number, and you don't have to use all cards. The phone numbers do not necessarily have to be distinct.

Input

The first line contains an integer nn — the number of cards with digits that you have (1≤n≤1001≤n≤100).

The second line contains a string of nn digits (characters "0", "1", ..., "9") s1,s2,…,sns1,s2,…,sn. The string will not contain any other characters, such as leading or trailing spaces.

Output

If at least one phone number can be made from these cards, output the maximum number of phone numbers that can be made. Otherwise, output 0.

Examples

input

Copy

11
00000000008

output

Copy

1

input

Copy

22
0011223344556677889988

output

Copy

2

input

Copy

11
31415926535

output

Copy

0

Note

In the first example, one phone number, "8000000000", can be made from these cards.

In the second example, you can make two phone numbers from the cards, for example, "80123456789" and "80123456789".

In the third example you can't make any phone number from the given cards.

题意:给你n位数 有两个数比较特殊 "80123456789"  和 "80000000000" 问n位数中能够组成多少个这种特殊数


 #include <iostream>
#include <string.h>
#define ll long long
using namespace std;
int c[100];
int main()
{
	int n;
	memset(c,0,sizeof(c));
	cin>>n;
	string s;
	cin>>s;
	int h=0;
	for(int i=0;i<s.size();i++)
	{
		int x=s[i]-'0';
		if(x==8)
		h++;
	}
	int minn=min(n/11,h);
	cout<<minn<<endl;
}

B. Maximum Sum of Digits

time limit per test

2 seconds

memory limit per test

512 megabytes

input

standard input

output

standard output

You are given a positive integer nn.

Let S(x)S(x) be sum of digits in base 10 representation of xx, for example, S(123)=1+2+3=6S(123)=1+2+3=6, S(0)=0S(0)=0.

Your task is to find two integers a,ba,b, such that 0≤a,b≤n0≤a,b≤n, a+b=na+b=n and S(a)+S(b)S(a)+S(b) is the largest possible among all such pairs.

Input

The only line of input contains an integer nn (1≤n≤1012)(1≤n≤1012).

Output

Print largest S(a)+S(b)S(a)+S(b) among all pairs of integers a,ba,b, such that 0≤a,b≤n0≤a,b≤n and a+b=na+b=n.

Examples

input

Copy

35

output

Copy

17

input

Copy

10000000000

output

Copy

91

Note

In the first example, you can choose, for example, a=17a=17 and b=18b=18, so that S(17)+S(18)=1+7+1+8=17S(17)+S(18)=1+7+1+8=17. It can be shown that it is impossible to get a larger answer.

In the second test example, you can choose, for example, a=5000000001a=5000000001 and b=4999999999b=4999999999, with S(5000000001)+S(4999999999)=91S(5000000001)+S(4999999999)=91. It can be shown that it is impossible to get a larger answer.

题意:给你一个n数 使其分成两个数 求分别将两个数的每位的值相加的最大值

题解:其中一个是为小于n的最大各位数值为9的数 另一个是n-这个数

#include <iostream>
#include <string.h>
#define ll long long
using namespace std;
int find(ll x)
{
	int h=0;
	while(x)
	{
		h+=x%10;
		x/=10;
	}
	return h;
}
int main()
{
	string s;
	cin>>s;
	//cout<<s.length();
	int flag=1;
	for(int i=0;i<s.length();i++)
	{
		if(s[i]!=9)
		{
			flag=0;
		} 
	}
	if(flag)
	{
		cout<<9*s.length()<<endl;
		
	}
	else
	{
		ll x=0;
		ll y=0;
		for(int i=0;i<s.length();i++)
		{
			x=x*10+s[i]-'0';
			if(i!=0)
			{
				y=y*10+9;
			}
		}
		cout<<find(x-y)+find(y)<<endl;
		//cout<<x<<" "<<y; 
	}
	return 0;
} 

C. Maximum Subrectangle

time limit per test

2 seconds

memory limit per test

512 megabytes

input

standard input

output

standard output

You are given two arrays aa and bb of positive integers, with length nn and mm respectively.

Let cc be an n×mn×m matrix, where ci,j=ai⋅bjci,j=ai⋅bj.

You need to find a subrectangle of the matrix cc such that the sum of its elements is at most xx, and its area (the total number of elements) is the largest possible.

Formally, you need to find the largest number ss such that it is possible to choose integers x1,x2,y1,y2x1,x2,y1,y2 subject to 1≤x1≤x2≤n1≤x1≤x2≤n, 1≤y1≤y2≤m1≤y1≤y2≤m, (x2−x1+1)×(y2−y1+1)=s(x2−x1+1)×(y2−y1+1)=s, and

∑i=x1x2∑j=y1y2ci,j≤x.∑i=x1x2∑j=y1y2ci,j≤x.

Input

The first line contains two integers nn and mm (1≤n,m≤20001≤n,m≤2000).

The second line contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤20001≤ai≤2000).

The third line contains mm integers b1,b2,…,bmb1,b2,…,bm (1≤bi≤20001≤bi≤2000).

The fourth line contains a single integer xx (1≤x≤2⋅1091≤x≤2⋅109).

Output

If it is possible to choose four integers x1,x2,y1,y2x1,x2,y1,y2 such that 1≤x1≤x2≤n1≤x1≤x2≤n, 1≤y1≤y2≤m1≤y1≤y2≤m, and ∑x2i=x1∑y2j=y1ci,j≤x∑i=x1x2∑j=y1y2ci,j≤x, output the largest value of (x2−x1+1)×(y2−y1+1)(x2−x1+1)×(y2−y1+1) among all such quadruplets, otherwise output 00.

Examples

input

Copy

3 3
1 2 3
1 2 3
9

output

Copy

4

input

Copy

5 1
5 4 2 4 5
2
5

output

Copy

1

Note

Matrix from the first sample and the chosen subrectangle (of blue color):

Matrix from the second sample and the chosen subrectangle (of blue color):

题意:给出两个数组A和B,他们能组成一个矩阵,然后求子矩阵的和<=X的,最大的子矩阵的面积。

题解:可以发现,一个子矩阵的值实际上就是这个子矩阵包括的aa和bb数组的乘积,根据乘法分配律可得。

所以可以预处理出长度一定时最小的a、ba、b区间,然后双指针扫描即可。

#include <iostream>
#include <string.h>
#include <algorithm>
#define ll long long
#define N 0x3f3f3f3f 
using namespace std;
ll a[4000];
ll b[4000];
ll suma[4000];
ll sumb[4000];
int main()
{
	ll n,m;
	cin>>n>>m;
	memset(suma,N,sizeof(suma));
	memset(sumb,N,sizeof(sumb));
	for(ll i=1;i<=n;i++)
	{
		cin>>a[i];
		a[i]+=a[i-1];
	}
	for(ll i=1;i<=m;i++)
	{
		cin>>b[i];
		b[i]+=b[i-1];
	}
	ll x;
	cin>>x;
	
	for(ll len=1;len<=n;len++)
	{
		for(ll i=1;i+len-1<=n;i++)
		{
			suma[len]=min(suma[len],a[i+len-1]-a[i-1]);
		}
	}
	
	for(ll len=1;len<=m;len++)
	{
		for(ll i=1;i+len-1<=m;i++)
		{
			sumb[len]=min(sumb[len],b[i+len-1]-b[i-1]);
		}
	}
	ll ans=0;
	
	for(ll i=1;i<=n;i++)
	{
		for(ll j=1;j<=m;j++)
		{
			if(suma[i]*sumb[j]<=x)
			{
				ans=max(ans,i*j);
			}
		}
	}
	cout<<ans<<endl;
	return 0;
}

D. Social Circles

time limit per test

2 seconds

memory limit per test

512 megabytes

input

standard input

output

standard output

You invited n guests to dinner! You plan to arrange one or more circles of chairs. Each chair is going to be either occupied by one guest, or be empty. You can make any number of circles.

Your guests happen to be a little bit shy, so the i-th guest wants to have a least li free chairs to the left of his chair, and at least ri free chairs to the right. The "left" and "right" directions are chosen assuming all guests are going to be seated towards the center of the circle. Note that when a guest is the only one in his circle, the li chairs to his left and ri chairs to his right may overlap.

What is smallest total number of chairs you have to use?

Input

First line contains one integer n  — number of guests, (1⩽n⩽105).

Next n lines contain n pairs of space-separated integers li and ri (0⩽li,ri⩽109).

Output

Output a single integer — the smallest number of chairs you have to use.

Examples

input

Copy

3
1 1
1 1
1 1

output

Copy

6

input

Copy

4
1 2
2 1
3 5
5 3

output

Copy

15

input

Copy

1
5 6

output

Copy

7

Note

In the second sample the only optimal answer is to use two circles: a circle with 5 chairs accomodating guests 1and 2, and another one with 10 chairs accomodationg guests 3 and 4.

In the third sample, you have only one circle with one person. The guest should have at least five free chairs to his left, and at least six free chairs to his right to the next person, which is in this case the guest herself. So, overall number of chairs should be at least 6+1=7.

题意:有n个人坐在一圈,每个人必须使他的左边空出li个椅子,右边空出ri个椅子。当只有一个人时,也必须满足这个要求。

题解:因为环可以有随便多个,自己和自己也可以成环,那就 记录l,和 r 然后两个数组sort一下,按下标配对就好了,配对取大的那个,所有对求和,然后加上n个人就是答案。

#include<iostream>
#include<algorithm>
#define ll long long
using namespace std;
int a[100010],b[100010];
int main()
{
	int n;
	cin>>n;
	for(int i=0;i<n;i++)
	{
		cin>>a[i]>>b[i];
	}
	sort(a,a+n);
	sort(b,b+n);
	ll h=n;
	for(int i=0;i<n;i++)
	{
		int c=max(a[i],b[i]);
		h+=c;
	}
	cout<<h<<endl;
	return 0;
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值