国庆个人赛——NO.2

1、

Codeforces——1038A( Equality )

You are given a string ss of length nn, which consists only of the first kk letters of the Latin alphabet. All letters in string ss are uppercase.

A subsequence of string ss is a string that can be derived from ss by deleting some of its symbols without changing the order of the remaining symbols. For example, "ADE" and "BD" are subsequences of "ABCDE", but "DEA" is not.

A subsequence of ss called good if the number of occurences of each of the first kk letters of the alphabet is the same.

Find the length of the longest good subsequence of ss.

Input

The first line of the input contains integers nn (1≤n≤1051≤n≤105) and kk (1≤k≤261≤k≤26).

The second line of the input contains the string ss of length nn. String ss only contains uppercase letters from 'A' to the kk-th letter of Latin alphabet.

Output

Print the only integer — the length of the longest good subsequence of string ss.

Examples

Input

9 3
ACAABCCAB

Output

6

Input

9 4
ABCABCABC

Output

0

Note

In the first example, "ACBCAB" ("ACAABCCAB") is one of the subsequences that has the same frequency of 'A', 'B' and 'C'. Subsequence "CAB" also has the same frequency of these letters, but doesn't have the maximum possible length.

In the second example, none of the subsequences can have 'D', hence the answer is 00.

题意:

给一个长度为 n ,只包含字典前 k 个大写字母的字符串 s

如果 s 的子序列中,k个字母出现的次数一样,那么这个子序列就是好的,问好的子序列的最大长度是多少

思路:

找出字典序前k个字母出现的最少次数,ans= minn× k

注意:当字符串中不包含前k个字母,ans=0

CODE:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <map>
#include <cstdlib>
#include <algorithm>
using namespace std;

typedef long long LL;
#define memset(a,n) memset(a,n,sizeof(a))
#define INF 0x3f3f3f3f
const int MAXX=1e5+10;

map<int,int>m;
int vis[MAXX];

int main()
{
    char s[MAXX];
    int n,k;
    scanf("%d %d",&n,&k);
    getchar();

    int flag=0;
    memset(vis,0);
    for(int i=0;i<n;i++)
    {
        scanf("%c",&s[i]);
        m[s[i]-'A']++;
        if(vis[s[i]-'A']==0){
            flag++;
            vis[s[i]-'A']=1;
        }
    }

//    for(int i=0;i<10;i++)
//        printf("%d ",m[i]);
//

    int minn;
    minn=INF;
    int t=0;

    if(flag<k)
        printf("0\n");
    else
    {
        for(int i=0;i<k;i++)
        {
            if(vis[i]==1)
            {
                minn=min(minn,m[i]);
            }
            else
            {
                t=1;
                break;
            }
        }


        if(t)
            printf("0\n");
        else
            printf("%d\n",minn*k);
    }
}

2、

Codeforces—— 1038B Non-Coprime Partition

Find out if it is possible to partition the first nn positive integers into two non-empty disjoint sets S1S1 and S2S2such that:

gcd(sum(S1),sum(S2))>1gcd(sum(S1),sum(S2))>1

Here sum(S)sum(S) denotes the sum of all elements present in set SS and gcdgcd means thegreatest common divisor.

Every integer number from 11 to nn should be present in exactly one of S1S1 or S2S2.

Input

The only line of the input contains a single integer nn (1≤n≤450001≤n≤45000)

Output

If such partition doesn't exist, print "No" (quotes for clarity).

Otherwise, print "Yes" (quotes for clarity), followed by two lines, describing S1S1 and S2S2 respectively.

Each set description starts with the set size, followed by the elements of the set in any order. Each set must be non-empty.

If there are multiple possible partitions — print any of them.

Examples

Input

1

Output

No

Input

3

Output

Yes
1 2
2 1 3 

Note

In the first example, there is no way to partition a single number into two non-empty sets, hence the answer is "No".

In the second example, the sums of the sets are 22 and 44 respectively. The gcd(2,4)=2>1gcd(2,4)=2>1, hence that is one of the possible answers.

题意:

给定 n 个数,选出某些数组成集合S1,剩余的数组成集合S2

如果 GCD(S1,S2)> 1 的话,按要求输出即可

思路:

样例输出有多种,可以指定 S1集合里边只有一位数,那么S2集合里存的是剩余的数

1、首先当 n=1||n=2 的时候,是不符合题意的,可以直接输出NO\2

2、sum(1-n)= n×(n+1)/ 2 ;

  遍历1-n中的数,如果 GCD( i,sum-i) > 1 的话,输出即可

CODE:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <map>
#include <cstdlib>
#include <algorithm>
using namespace std;

typedef long long LL;
#define memset(a,n) memset(a,n,sizeof(a))
#define INF 0x3f3f3f3f

int vis[45000+10];


int gcd(int a,int b)
{
    if(b==0)
        return a;
    return gcd(b,a%b);
}
int main()
{
    int n;
    int sum=0;
    scanf("%d",&n);

    memset(vis,0);

    int x,k,kk;
    int flag=0;

    kk=1;

    if(n==1||n==2)
        printf("No\n");

    else
    {
        sum=(1+n)*n/2;

        for(int i=1; i<=n; i++)
        {
            x=sum-i;
            if(gcd(i,x)>1)
            {
                flag=1;
                k=i;
                break;
            }
        }

        if(flag)
        {
            printf("Yes\n");
            printf("%d %d\n",kk,k);
            printf("%d",n-1);
            for(int i=1;i<=n;i++)
            {
                if(i!=k)
                    printf("% d",i);
            }
            printf("\n");
        }
        else
            printf("No\n");

    }

}

3、

Codefo——1038C Gambling(贪心)

Two players A and B have a list of nn integers each. They both want to maximize the subtraction between their score and their opponent's score.

In one turn, a player can either add to his score any element from his list (assuming his list is not empty), the element is removed from the list afterward. Or remove an element from his opponent's list (assuming his opponent's list is not empty).

Note, that in case there are equal elements in the list only one of them will be affected in the operations above. For example, if there are elements {1,2,2,3}{1,2,2,3} in a list and you decided to choose 22 for the next turn, only a single instance of 22 will be deleted (and added to the score, if necessary).

The player A starts the game and the game stops when both lists are empty. Find the difference between A's score and B's score at the end of the game, if both of the players are playing optimally.

Optimal play between two players means that both players choose the best possible strategy to achieve the best possible outcome for themselves. In this problem, it means that each player, each time makes a move, which maximizes the final difference between his score and his opponent's score, knowing that the opponent is doing the same.

Input

The first line of input contains an integer nn (1≤n≤1000001≤n≤100000) — the sizes of the list.

The second line contains nn integers aiai (1≤ai≤1061≤ai≤106), describing the list of the player A, who starts the game.

The third line contains nn integers bibi (1≤bi≤1061≤bi≤106), describing the list of the player B.

Output

Output the difference between A's score and B's score (A−BA−B) if both of them are playing optimally.

Examples

Input

2
1 4
5 1

Output

0

Input

3
100 100 100
100 100 100

Output

0

Input

2
2 1
5 6

Output

-3

Note

In the first example, the game could have gone as follows:

  • A removes 55 from B's list.
  • B removes 44 from A's list.
  • A takes his 11.
  • B takes his 11.

Hence, A's score is 11, B's score is 11 and difference is 00.

There is also another optimal way of playing:

  • A removes 55 from B's list.
  • B removes 44 from A's list.
  • A removes 11 from B's list.
  • B removes 11 from A's list.

The difference in the scores is still 00.

In the second example, irrespective of the moves the players make, they will end up with the same number of numbers added to their score, so the difference will be 00.

题意:

A B 两人各有一列数字,每进行一轮游戏,A B 都可以选择去掉对方中的某一个数,或者是从自己列表中选择一个数字加到自己的分数里边,并且两人都是采取的最好的策略,每一轮游戏的时候都想使自己的分数尽可能的大,最后输出 A-B 的值

思路:

先将列表中得数由大到小排序,每进行一轮游戏的时候,如果对方得数比自己当前的数字大,就选择将对方的数字去掉

否则,将该数字加到自己的分数上边(每一轮都采取最好的策略,最好使自己的分数尽可能的大)

CODE:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <map>
#include <cstdlib>
#include <algorithm>
using namespace std;

typedef long long LL;
#define memset(a,n) memset(a,n,sizeof(a))
#define INF 0x3f3f3f3f

LL a[100000+10];
LL b[100000+10];

bool cmp(LL a,LL b)
{
    return a>b;
}
bool cmp1(LL a,LL b)
{
    return a>b;
}
int main()
{
    LL n;
    scanf("%lld",&n);

    for(LL i=0;i<n;i++)
        scanf("%lld",&a[i]);
    for(LL i=0;i<n;i++)
        scanf("%lld",&b[i]);

    LL cnt=0;
    LL i,j;
    i=0;
    j=0;
    LL sum=0,sum1=0;

    sort(a,a+n,cmp);
    sort(b,b+n,cmp1);
    while(i<n||j<n)
    {
        if(cnt%2==0)
        {
            if(a[i]<=b[j])
                j++;
            else
            {
                sum+=a[i];
                i++;
            }
        }
        else
        {
            if(a[i]>=b[j])
                i++;
            else
            {
                sum1+=b[j];
                j++;
            }
        }
        cnt++;
    }
    printf("%lld",sum-sum1);
}

4、

Codeforces——1040A Palindrome Dance(回文)

A group of nn dancers rehearses a performance for the closing ceremony. The dancers are arranged in a row, they've studied their dancing moves and can't change positions. For some of them, a white dancing suit is already bought, for some of them — a black one, and for the rest the suit will be bought in the future.

On the day when the suits were to be bought, the director was told that the participants of the olympiad will be happy if the colors of the suits on the scene will form a palindrome. A palindrome is a sequence that is the same when read from left to right and when read from right to left. The director liked the idea, and she wants to buy suits so that the color of the leftmost dancer's suit is the same as the color of the rightmost dancer's suit, the 2nd left is the same as 2nd right, and so on.

The director knows how many burls it costs to buy a white suit, and how many burls to buy a black suit. You need to find out whether it is possible to buy suits to form a palindrome, and if it's possible, what's the minimal cost of doing so. Remember that dancers can not change positions, and due to bureaucratic reasons it is not allowed to buy new suits for the dancers who already have suits, even if it reduces the overall spending.

Input

The first line contains three integers nn, aa, and bb (1≤n≤201≤n≤20, 1≤a,b≤1001≤a,b≤100) — the number of dancers, the cost of a white suit, and the cost of a black suit.

The next line contains nn numbers cici, ii-th of which denotes the color of the suit of the ii-th dancer. Number 00 denotes the white color, 11 — the black color, and 22 denotes that a suit for this dancer is still to be bought.

Output

If it is not possible to form a palindrome without swapping dancers and buying new suits for those who have one, then output -1. Otherwise, output the minimal price to get the desired visual effect.

Examples

Input

5 100 1
0 1 2 1 2

Output

101

Input

3 10 12
1 2 0

Output

-1

Input

3 12 1
0 1 0

Output

0

Note

In the first sample, the cheapest way to obtain palindromic colors is to buy a black suit for the third from left dancer and a white suit for the rightmost dancer.

In the second sample, the leftmost dancer's suit already differs from the rightmost dancer's suit so there is no way to obtain the desired coloring.

In the third sample, all suits are already bought and their colors form a palindrome.

题意:

给定一个字符串,已知有些位置上是'W'(用 0表示) ,有些位置上是'B'(用1表示),有些位置不确定(用2表示)

你的任务是给 不确定位置上 (2)买一些衣服,使得该字符串是一个回文串,并且要求所花费的价钱最少

已知买白色需要a,买黑色需要b

思路:

分类讨论(暴力)

1、1个人的时候,如果是0、1已经确定好得数,不需要花费,如果是2,花费min(a,b)

2、如果首位与末位不相等的话,不能构成回文数

3、讨论对应位置取值情况 (循环跑对称)

  0 0 

  1 1     以上两种不需要花费

  0 2

  2 0     以上两种需要花费 a

  1 2

  2 1     以上两种需要花费 b

  2 2     需要花费 2×min(a,b)

  0 1

  1 0     以上两种不满足题意

4、当n是奇数的时候,对称位置在循环中是取不到的,所以特判一下对称位置的数值

CODE:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <map>
#include <cstdlib>
#include <algorithm>
using namespace std;

typedef long long LL;
#define memset(a,n) memset(a,n,sizeof(a))
#define INF 0x3f3f3f3f

int main()
{
    int n,x,y;
    int a[50];

    scanf("%d %d %d",&n,&x,&y);

    for(int i=0; i<n; i++)
        scanf("%d",&a[i]);

    int t=0;
    int sum=0;

    if(n==1)
    {
        if(a[0]==0||a[0]==1)
            printf("0\n");
        else
            printf("%d\n",min(x,y));
    }

    else
    {
        if((a[0]==0&&a[n-1]==1)||(a[0]==1&&a[n-1]==0))
            printf("-1\n");

        else
        {
            for(int i=0; i<n/2;i++)
            {
                if((a[i]==0&&a[n-i-1]==1)||(a[i]==1&&a[n-i-1]==0))
                {
                    t=1;
                    break;
                }

                if((a[i]==0&&a[n-i-1]==0)||(a[i]==1&&a[n-i-1]==1))
                    continue;
                if((a[i]==0&&a[n-i-1]==2)||(a[i]==2&&a[n-i-1]==0))
                    sum+=x;

                if((a[i]==1&&a[n-i-1]==2)||(a[i]==2&&a[n-i-1]==1))
                    sum+=y;
                if(a[i]==a[n-i-1]&&a[i]==2)
                    sum+=2*min(x,y);
            }

            if(n%2){
                if(a[n/2]==2)
                    sum+=min(x,y);
            }

            if(t)
                printf("-1\n");
            else
                printf("%d\n",sum);
        }
    }
}

5、

Codefor——1040BShashlik Cooking (贪心 + 细节)

Long story short, shashlik is Miroslav's favorite food. Shashlik is prepared on several skewers simultaneously. There are two states for each skewer: initial and turned over.

This time Miroslav laid out nn skewers parallel to each other, and enumerated them with consecutive integers from 11 to nn in order from left to right. For better cooking, he puts them quite close to each other, so when he turns skewer number ii, it leads to turning kk closest skewers from each side of the skewer ii, that is, skewers number i−ki−k, i−k+1i−k+1, ..., i−1i−1, i+1i+1, ..., i+k−1i+k−1, i+ki+k (if they exist).

For example, let n=6n=6 and k=1k=1. When Miroslav turns skewer number 33, then skewers with numbers 22, 33, and 44 will come up turned over. If after that he turns skewer number 11, then skewers number 11, 33, and 44 will be turned over, while skewer number 22 will be in the initial position (because it is turned again).

As we said before, the art of cooking requires perfect timing, so Miroslav wants to turn over all nnskewers with the minimal possible number of actions. For example, for the above example n=6n=6 and k=1k=1, two turnings are sufficient: he can turn over skewers number 22 and 55.

Help Miroslav turn over all nn skewers.

Input

The first line contains two integers nn and kk (1≤n≤10001≤n≤1000, 0≤k≤10000≤k≤1000) — the number of skewers and the number of skewers from each side that are turned in one step.

Output

The first line should contain integer ll — the minimum number of actions needed by Miroslav to turn over all nn skewers. After than print ll integers from 11 to nn denoting the number of the skewer that is to be turned over at the corresponding step.

Examples

Input

7 2

Output

2
1 6 

Input

5 1

Output

2
1 4 

Note

In the first example the first operation turns over skewers 11, 22 and 33, the second operation turns over skewers 44, 55, 66 and 77.

In the second example it is also correct to turn over skewers 22 and 55, but turning skewers 22 and 44, or 11and 55 are incorrect solutions because the skewer 33 is in the initial state after these operations.

猜你喜欢

转载自blog.csdn.net/JKdd123456/article/details/82926749