2018国庆第二场个人赛

版权声明:转载时 别忘了注明出处 https://blog.csdn.net/ZCY19990813/article/details/82927048

CodeForces - 1040A  

A group of n 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 n, a, and b (1≤n≤20, 1≤a,b≤100) — the number of dancers, the cost of a white suit, and the cost of a black suit.

The next line contains n numbers ci, i-th of which denotes the color of the suit of the i-th dancer. Number 0 denotes the white color, 1 — the black color, and 2 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

扫描二维码关注公众号,回复: 3437179 查看本文章
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.

 题意:有n个人 白衣服的花费a元  黑衣服的花费b元    下面有n个数 0是穿着白衣服 1是穿着黑衣服 2是准备要买的衣服

要求最后白黑衣服是对称的 问最小的花费

#include <iostream>
#include <algorithm>
#include<cstdio>
#include<stack>
#include <deque>
#include <cstdlib>
#include <cstring>
#include <string>
#include <map>
#include <deque>
#include <vector>
using namespace std;
typedef long long ll;
map<char,ll> M;
int s[100];
int main()
{
    int n,a,b,i,ans=0;
    cin>>n>>a>>b;
    for(i=0; i<n; i++)
        cin>>s[i];
    int  p=min(a,b);
    if(n==1)
    {
        if(s[0]==0||s[0]==1)
            cout<<"0"<<endl;
        else
            cout<<p<<endl;
    }
    else
    {
        for(i=0;i<n/2;i++)
        {
            if(s[i]==1&&s[n-i-1]==1||s[i]==0&&s[n-i-1]==0)
                continue;
            else
            if(s[i]==1&&s[n-i-1]==0||s[i]==0&&s[n-i-1]==1)
               break;
            else
            if(s[i]==0&&s[n-i-1]==2||s[i]==2&&s[n-i-1]==0)
                 ans+=a;
            else
            if(s[i]==1&&s[n-i-1]==2||s[i]==2&&s[n-i-1]==1)
                ans+=b;
            else
            if(s[i]==2&&s[n-i-1]==2)
                ans+=2*p;
        }
        if(n%2!=0)
        {
            if(s[n/2]==2)
                ans+=p;
        }
        if(i>=n/2)
            cout<<ans<<endl;
        else
            cout<<"-1"<<endl;
    }

    return 0;
}

CodeForces - 1038A  

You are given a string s of length n, which consists only of the first k letters of the Latin alphabet. All letters in string s

are uppercase.A subsequence of string s is a string that can be derived from s 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 s called good if the number of occurences of each of the first k letters of the alphabet is the same.

Find the length of the longest good subsequence of s.

Input

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

The second line of the input contains the string s of length n. String s only contains uppercase letters from 'A' to the k-th letter of Latin alphabet.

Output

Print the only integer — the length of the longest good subsequence of string s

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 0

.题意:n字符串的长度 m前几个字母 找不重复的字母有几组×m

思路:先看有没有m个不同的字母 没有就ans=0 否则遍历一遍 找最少的字母个数×m

#include <iostream>
#include <algorithm>
#include<cstdio>
#include<stack>
#include <deque>
#include <cstdlib>
#include <cstring>
#include <string>
#include <map>
#include <deque>
#include <vector>
using namespace std;
typedef long long ll;
char s[101010];
ll flag[27]={0};
map<char,ll> M;
int main()
{
    ll n,i,j,m,sum=0,p;
    cin>>n>>m;
    cin>>s;
    char c;
    for(i=0;i<n;i++)
        M[s[i]]++;
    map<char,ll>::iterator it;
    sum=0;
    for(it=M.begin();it!=M.end();it++)
    {
        if(it->second>=0)
            sum++;
    }
    if(sum==m)
    {
        ll minn=1110000;
        for(it=M.begin();it!=M.end();it++)
        {
           if(it->second<=minn)
            minn=it->second;
        }
        cout<<m*minn<<endl;
    }
    else
        cout<<"0"<<endl;
    return 0;
}

CodeForces - 1038B

Find out if it is possible to partition the first n positive integers into two non-empty disjoint sets S1 and S2

such that:gcd(sum(S1),sum(S2))>1 Here sum(S)denotes the sum of all elements present in set S and gcd

means thegreatest common divisor.Every integer number from 1 to n should be present in exactly one of S1 or S2.

Input

The only line of the input contains a single integer n(1≤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 S1 and S2

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 2

and 4 respectively. The gcd(2,4)=2>1, hence that is one of the possible answers.

思路:n小于等于2 NO  其余情况都满足  然后奇数一个集合 偶数一个集合

#include <iostream>
#include <algorithm>
#include<cstdio>
#include<stack>
#include <deque>
#include <cstdlib>
#include <cstring>
#include <string>
#include <map>
#include <deque>
#include <vector>
using namespace std;
typedef long long ll;
map<char,ll> M;
int main()
{
    ll n,i,c=0,d=0;
    ll ans1[45010]={0};
    ll ans2[45010]={0};
    cin>>n;
    if(n<=2)
        cout<<"No"<<endl;
    else
    {
        for(i=1;i<=n;i++)
        {
            if(i%2==0)
                ans1[c++]=i;
            else
                ans2[d++]=i;
        }
        cout<<"Yes"<<endl;
        cout<<c<<" ";
        cout<<ans1[0];
        for(i=1;i<c;i++)
            cout<<" "<<ans1[i];
        cout<<endl;
        cout<<d<<" "<<ans2[0];
        for(i=1;i<d;i++)
            cout<<" "<<ans2[i];
        cout<<endl;
    }
    return 0;
}

CodeForces - 1038C

 Two players A and B have a list of n 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}in a list and you decided to choose 2 for the next turn, only a single instance of 2 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 n(1≤n≤100000) — the sizes of the list.The second line contains n integers ai (1≤ai≤106), describing the list of the player A, who starts the game.The third line contains nintegers bi (1≤bi≤106), describing the list of the player B.

Output

Output the difference between A's score and B's score (A−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

Output

-3

Note

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

  • A removes 5from B's list.
  • B removes 4from A's list.
  • A takes his 1.
  • B takes his 1.

Hence, A's score is 1, B's score is 1 and difference is 0.

There is also another optimal way of playing:

  • A removes 5from B's list.
  • B removes 4from A's list.
  • A removes 1from B's list.
  • B removes 1from A's list.

The difference in the scores is still 0.

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 0.

 题意:AB两个人玩游戏 A的目标是让SA-SB最大  B的目标是最小  他们有两种选择  扔掉对方的 或者出自己的牌

思路:按从大到小排序 要是第一个小于B的第一个 就扔掉对方的 否则将自己的牌打出  用两个指针ij标记现在的位置

#include <iostream>
#include <algorithm>
#include<cstdio>
#include<stack>
#include <deque>
#include <cstdlib>
#include <cstring>
#include <string>
#include <map>
#include <deque>
#include <vector>
using namespace std;
typedef long long ll;
map<char,ll> M;
ll cmp(ll p,ll q)
{
    return p>q;
}
ll a[101100]={0},b[100100]={0};
int main()
{
    ll n;
    ll i=0,j=0,s1=0,s2=0;
    cin>>n;
    for(i=0;i<n;i++)
        cin>>a[i];
    for(j=0;j<n;j++)
        cin>>b[j];
    sort(a,a+n,cmp);
    sort(b,b+n,cmp);
    i=0,j=0;
    while(1)
    {
        if(i>=n&&j>=n)
            break;
        if(a[i]>=b[j])
        {

            s1+=a[i];
            i++;
        }
        else
            j++;
        if(i>=n&&j>=n)
            break;
        if(b[j]>=a[i])
        {

            s2+=b[j]; j++;
        }
        else
            i++;
    }
    cout<<s1-s2<<endl;
    return 0;
}

猜你喜欢

转载自blog.csdn.net/ZCY19990813/article/details/82927048