20211013

目录

A - Drinks Choosing

题意:

思路:

代码:

B - Sport Mafia

题意:

思路:

代码:

C - Basketball Exercise

题意:

思路:

代码:


A - Drinks Choosing

Old timers of Summer Informatics School can remember previous camps in which each student was given a drink of his choice on the vechorka (late-evening meal). Or may be the story was more complicated?

There are nn students living in a building, and for each of them the favorite drink a_iai​ is known. So you know nn integers a_1, a_2, \dots, a_na1​,a2​,…,an​, where a_iai​ (1 \le a_i \le k1≤ai​≤k) is the type of the favorite drink of the ii-th student. The drink types are numbered from 11 to kk.

There are infinite number of drink sets. Each set consists of exactly two portions of the same drink. In other words, there are kk types of drink sets, the jj-th type contains two portions of the drink jj. The available number of sets of each of the kk types is infinite.

You know that students will receive the minimum possible number of sets to give all students exactly one drink. Obviously, the number of sets will be exactly \lceil \frac{n}{2} \rceil⌈2n​⌉, where \lceil x \rceil⌈x⌉ is xx rounded up.

After students receive the sets, they will distribute their portions by their choice: each student will get exactly one portion. Note, that if nn is odd then one portion will remain unused and the students' teacher will drink it.

What is the maximum number of students that can get their favorite drink if \lceil \frac{n}{2} \rceil⌈2n​⌉ sets will be chosen optimally and students will distribute portions between themselves optimally?

Input

The first line of the input contains two integers nn and kk (1 \le n, k \le 1\,0001≤n,k≤1000) — the number of students in the building and the number of different drinks.

The next nn lines contain student's favorite drinks. The ii-th line contains a single integer from 11 to kk — the type of the favorite drink of the ii-th student.

Output

Print exactly one integer — the maximum number of students that can get a favorite drink.

Examples

Input

5 3
1
3
1
1
2

Output

4

Input

10 3
2
1
3
2
3
3
1
3
1
2

Output

9

Note

In the first example, students could choose three sets with drinks 11, 11 and 22 (so they will have two sets with two drinks of the type 11 each and one set with two drinks of the type 22, so portions will be 1, 1, 1, 1, 2, 21,1,1,1,2,2). This way all students except the second one will get their favorite drinks.

Another possible answer is sets with drinks 11, 22 and 33. In this case the portions will be 1, 1, 2, 2, 3, 31,1,2,2,3,3. Then all the students except one will gain their favorite drinks. The only student that will not gain the favorite drink will be a student with a_i = 1ai​=1 (i.e. the first, the third or the fourth).

题意:

有n个学生和k种饮料,输入每个学生需要的饮料的种类;现在可以最多拿n/2个饮料集合,每个饮料集合有两个相同种类的水,每种饮料集合有无限个,问最多有多少个学生的需求被满足。

思路:

先算出每种饮料的需求数,然后看有多少个需求数是奇数,将奇数的数量除以2就是无法被满足的学生的数量,用n减去得到答案。

代码:

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<vector>
#include<algorithm>
#include<map>
using namespace std;
typedef long long ll;
bool cmp(int a,int b)
{
    return a>b;
}
int main()
{
    ll n,k;
    cin>>n>>k;
    map<int,int>mp;
    for(int i=0;i<n;i++)
    {
        int t;
        cin>>t;
        if(mp.find(t)!=mp.end())
        {
            mp[t]+=1;
        }
        else
        {
            mp[t]=1;
        }
    }
    map<int,int>::iterator it;
    vector<int> ve;
    ll ans=0;
    for(it=mp.begin();it!=mp.end();it++)
    {
        if((it->second)%2!=0)
        {
            ans+=1;
        }
    }
    ans/=2;
    cout<<n-ans<<endl;
}

B - Sport Mafia

Each evening after the dinner the SIS's students gather together to play the game of Sport Mafia.

For the tournament, Alya puts candies into the box, which will serve as a prize for a winner. To do that, she performs nn actions. The first action performed is to put a single candy into the box. For each of the remaining moves she can choose from two options:

  • the first option, in case the box contains at least one candy, is to take exactly one candy out and eat it. This way the number of candies in the box decreased by 11;
  • the second option is to put candies in the box. In this case, Alya will put 11 more candy, than she put in the previous time.

Thus, if the box is empty, then it can only use the second option.

For example, one possible sequence of Alya's actions look as follows:

  • put one candy into the box;
  • put two candies into the box;
  • eat one candy from the box;
  • eat one candy from the box;
  • put three candies into the box;
  • eat one candy from the box;
  • put four candies into the box;
  • eat one candy from the box;
  • put five candies into the box;

This way she will perform 99 actions, the number of candies at the end will be 1111, while Alya will eat 44 candies in total.

You know the total number of actions nn and the number of candies at the end kk. You need to find the total number of sweets Alya ate. That is the number of moves of the first option. It's guaranteed, that for the given nn and kk the answer always exists.

Please note, that during an action of the first option, Alya takes out and eats exactly one candy.

Input

The first line contains two integers nn and kk (1 \le n \le 10^91≤n≤109; 0 \le k \le 10^90≤k≤109) — the total number of moves and the number of candies in the box at the end.

It's guaranteed, that for the given nn and kk the answer exists.

Output

Print a single integer — the number of candies, which Alya ate. Please note, that in this problem there aren't multiple possible answers — the answer is unique for any input data.

Examples

Input

1 1

Output

0

Input

9 11

Output

4

Input

5 0

Output

3

Input

3 2

Output

1

Note

In the first example, Alya has made one move only. According to the statement, the first move is always putting one candy in the box. Hence Alya ate 00 candies.

In the second example the possible sequence of Alya's actions looks as follows:

  • put 11 candy,
  • put 22 candies,
  • eat a candy,
  • eat a candy,
  • put 33 candies,
  • eat a candy,
  • put 44 candies,
  • eat a candy,
  • put 55 candies.

This way, she will make exactly n=9n=9 actions and in the end the box will contain 1+2-1-1+3-1+4-1+5=111+2−1−1+3−1+4−1+5=11 candies. The answer is 44, since she ate 44 candies in total.

题意:

有n次操作,使得最终的糖果数量为k;每次操作可以有两种选择:1、放入比上一次放入数量+1个糖果;2、吃掉一个糖果。问吃了几个糖果。

思路:

假设放入a次糖果,吃了b次糖果,则:

a+b=n

a*(a+1)/2-b=k

联立得

a*(a+3)=2*(n+k)

二分查找方程的解得到a即可算出b。

代码:

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<vector>
#include<algorithm>
#include<map>
#include<cmath>
using namespace std;
typedef long long ll;
int main()
{
    ll n,k;
    cin>>n>>k;
    ll low=0;
    ll high=5e9;
    ll a=(low+high)/2;
    while(low<high)
    {
        if(a*(a+3)<2*(n+k))
        {
            low=a;
        }
        else if(a*(a+3)>2*(n+k))
        {
            high=a;
        }
        else
            break;
        a=(low+high)/2;
    }
    cout<<n-a<<endl;
}

C - Basketball Exercise

Finally, a basketball court has been opened in SIS, so Demid has decided to hold a basketball exercise session. 2 \cdot n2⋅n students have come to Demid's exercise session, and he lined up them into two rows of the same size (there are exactly nn people in each row). Students are numbered from 11 to nn in each row in order from left to right.

Now Demid wants to choose a team to play basketball. He will choose players from left to right, and the index of each chosen player (excluding the first one taken) will be strictly greater than the index of the previously chosen player. To avoid giving preference to one of the rows, Demid chooses students in such a way that no consecutive chosen students belong to the same row. The first student can be chosen among all 2n2n students (there are no additional constraints), and a team can consist of any number of students.

Demid thinks, that in order to compose a perfect team, he should choose students in such a way, that the total height of all chosen students is maximum possible. Help Demid to find the maximum possible total height of players in a team he can choose.

Input

The first line of the input contains a single integer nn (1 \le n \le 10^51≤n≤105) — the number of students in each row.

The second line of the input contains nn integers h_{1, 1}, h_{1, 2}, \ldots, h_{1, n}h1,1​,h1,2​,…,h1,n​ (1 \le h_{1, i} \le 10^91≤h1,i​≤109), where h_{1, i}h1,i​ is the height of the ii-th student in the first row.

The third line of the input contains nn integers h_{2, 1}, h_{2, 2}, \ldots, h_{2, n}h2,1​,h2,2​,…,h2,n​ (1 \le h_{2, i} \le 10^91≤h2,i​≤109), where h_{2, i}h2,i​ is the height of the ii-th student in the second row.

Output

Print a single integer — the maximum possible total height of players in a team Demid can choose.

Examples

Input

5
9 3 5 7 3
5 8 1 4 5

Output

29

Input

3
1 2 9
10 1 1

Output

19

Input

1
7
4

Output

7

Note

In the first example Demid can choose the following team as follows:

In the second example Demid can choose the following team as follows:

题意:

有两队学生,每个队有n个学生编号为1~n,输入每个学生的身高,现在从两个队中挑选学生组成球队按以下规则:不能连续选同一个队的学生且编号不能重复。问球队的身高和最大是多少。

思路:

动态规划,dpa[i]=max(dpa[i-1],a[i]+dpb[i-1]),每个dpa[i]代表编号1~i的一个可能的最大值,dpb[i]同理,最后取dpa[n]和dpb[n]中的最大值即可。

代码:

#include<iostream>
#include<algorithm>
#include<string>
#include<algorithm>
#include<map>
#include<vector>
#include<cstdlib>
using namespace std;
typedef long long ll;
ll a[1000000];
ll b[1000000];
ll dpa[1000000];
ll dpb[1000000];
int main()
{
    ll n;
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        cin>>a[i];
    }
    for(int i=1;i<=n;i++)
    {
        cin>>b[i];
    }
    for(int i=1;i<=n;i++)
    {
        dpa[i]=max(dpa[i-1],a[i]+dpb[i-1]);
        dpb[i]=max(dpb[i-1],b[i]+dpa[i-1]);
    }
    cout<<max(dpa[n],dpb[n])<<endl;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值