Codeforces Round #376 (Div. 2)

A. Night at the Museum
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Grigoriy, like the hero of one famous comedy film, found a job as a night security guard at the museum. At first night he receivedembosser and was to take stock of the whole exposition.

Embosser is a special devise that allows to "print" the text of a plastic tape. Text is printed sequentially, character by character. The device consists of a wheel with a lowercase English letters written in a circle, static pointer to the current letter and a button that print the chosen letter. At one move it's allowed to rotate the alphabetic wheel one step clockwise or counterclockwise. Initially, static pointer points to letter 'a'. Other letters are located as shown on the picture:

After Grigoriy add new item to the base he has to print its name on the plastic tape and attach it to the corresponding exhibit. It's not required to return the wheel to its initial position with pointer on the letter 'a'.

Our hero is afraid that some exhibits may become alive and start to attack him, so he wants to print the names as fast as possible. Help him, for the given string find the minimum number of rotations of the wheel required to print it.

Input

The only line of input contains the name of some exhibit — the non-empty string consisting of no more than 100 characters. It's guaranteed that the string consists of only lowercase English letters.

Output

Print one integer — the minimum number of rotations of the wheel, required to print the name given in the input.

Examples
input
zeus
output
18
input
map
output
35
input
ares
output
34
Note

 

To print the string from the first sample it would be optimal to perform the following sequence of rotations:

  1. from 'a' to 'z' (1 rotation counterclockwise),
  2. from 'z' to 'e' (5 clockwise rotations),
  3. from 'e' to 'u' (10 rotations counterclockwise),
  4. from 'u' to 's' (2 counterclockwise rotations).
In total,   1 + 5 + 10 + 2 = 18  rotations are required.

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
char str[105];
int main()
{
    while(~scanf("%s",str))
    {
        int len = strlen(str);
        int sum = 0;
        char temp = 'a';
        for(int i = 0;i < len;i++)
        {
            sum += min( abs(str[i]-temp) , abs(26-abs(str[i]-temp)) );
            temp = str[i];
        }
        printf("%d\n",sum);
    }
    return 0;
}

B. Coupons and Discounts
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

The programming competition season has already started and it's time to train for ICPC. Sereja coaches his teams for a number of year and he knows that to get ready for the training session it's not enough to prepare only problems and editorial. As the training sessions lasts for several hours, teams become hungry. Thus, Sereja orders a number of pizzas so they can eat right after the end of the competition.

Teams plan to train for n times during n consecutive days. During the training session Sereja orders exactly one pizza for each team that is present this day. He already knows that there will be ai teams on the i-th day.

There are two types of discounts in Sereja's favourite pizzeria. The first discount works if one buys two pizzas at one day, while the second is a coupon that allows to buy one pizza during two consecutive days (two pizzas in total).

As Sereja orders really a lot of pizza at this place, he is the golden client and can use the unlimited number of discounts and coupons of any type at any days.

Sereja wants to order exactly ai pizzas on the i-th day while using only discounts and coupons. Note, that he will never buy more pizzas than he need for this particular day. Help him determine, whether he can buy the proper amount of pizzas each day if he is allowed to use only coupons and discounts. Note, that it's also prohibited to have any active coupons after the end of the day n.

Input

The first line of input contains a single integer n (1 ≤ n ≤ 200 000) — the number of training sessions.

The second line contains n integers a1a2, ..., an (0 ≤ ai ≤ 10 000) — the number of teams that will be present on each of the days.

Output

If there is a way to order pizzas using only coupons and discounts and do not buy any extra pizzas on any of the days, then print "YES" (without quotes) in the only line of output. Otherwise, print "NO" (without quotes).

Examples
input
4
1 2 1 2
output
YES
input
3
1 0 1
output
NO
Note

In the first sample, Sereja can use one coupon to buy one pizza on the first and the second days, one coupon to buy pizza on the second and the third days and one discount to buy pizzas on the fourth days. This is the only way to order pizzas for this sample.

In the second sample, Sereja can't use neither the coupon nor the discount without ordering an extra pizza. Note, that it's possible that there will be no teams attending the training sessions on some days.

  做这类题目应该先思考好具体方法再去实现啊,不然会导致思路混乱中断什么的!我参考了大佬们简单实现的代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N = 200005;
int a[N];

int main()
{
    int n;
    cin >> n;
    for(int i = 1;i <= n;i++)
        scanf("%d",&a[i]);

    for(int i = 1;i <= n;i++)
    {
        if(a[i] < 0)
        {
            printf("NO\n");
            return 0;
        }
        if(a[i] % 2)
            a[i+1]--;
    }
    if(a[n+1] != 0)
        printf("NO\n");
    else
        printf("YES\n");
    return 0;
}

C. Socks
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Arseniy is already grown-up and independent. His mother decided to leave him alone for m days and left on a vacation. She have prepared a lot of food, left some money and washed all Arseniy's clothes.

Ten minutes before her leave she realized that it would be also useful to prepare instruction of which particular clothes to wear on each of the days she will be absent. Arseniy's family is a bit weird so all the clothes is enumerated. For example, each of Arseniy's n socks is assigned a unique integer from 1 to n. Thus, the only thing his mother had to do was to write down two integers li and ri for each of the days — the indices of socks to wear on the day i (obviously, li stands for the left foot and ri for the right). Each sock is painted in one ofk colors.

When mother already left Arseniy noticed that according to instruction he would wear the socks of different colors on some days. Of course, that is a terrible mistake cause by a rush. Arseniy is a smart boy, and, by some magical coincidence, he posses k jars with the paint — one for each of k colors.

Arseniy wants to repaint some of the socks in such a way, that for each of m days he can follow the mother's instructions and wear the socks of the same color. As he is going to be very busy these days he will have no time to change the colors of any socks so he has to finalize the colors now.

The new computer game Bota-3 was just realised and Arseniy can't wait to play it. What is the minimum number of socks that need their color to be changed in order to make it possible to follow mother's instructions and wear the socks of the same color during each of mdays.

Input

The first line of input contains three integers nm and k (2 ≤ n ≤ 200 0000 ≤ m ≤ 200 0001 ≤ k ≤ 200 000) — the number of socks, the number of days and the number of available colors respectively.

The second line contain n integers c1c2, ..., cn (1 ≤ ci ≤ k) — current colors of Arseniy's socks.

Each of the following m lines contains two integers li and ri (1 ≤ li, ri ≤ nli ≠ ri) — indices of socks which Arseniy should wear during the i-th day.

Output

Print one integer — the minimum number of socks that should have their colors changed in order to be able to obey the instructions and not make people laugh from watching the socks of different colors.

Examples
input
3 2 3
1 2 3
1 2
2 3
output
2
input
3 2 2
1 1 2
1 2
2 1
output
0
Note

In the first sample, Arseniy can repaint the first and the third socks to the second color.

In the second sample, there is no need to change any colors.



F. Video Cards
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Little Vlad is fond of popular computer game Bota-2. Recently, the developers announced the new add-on named Bota-3. Of course, Vlad immediately bought only to find out his computer is too old for the new game and needs to be updated.

There are n video cards in the shop, the power of the i-th video card is equal to integer value ai. As Vlad wants to be sure the new game will work he wants to buy not one, but several video cards and unite their powers using the cutting-edge technology. To use this technology one of the cards is chosen as the leading one and other video cards are attached to it as secondary. For this new technology to work it's required that the power of each of the secondary video cards is divisible by the power of the leading video card. In order to achieve that the power of any secondary video card can be reduced to any integer value less or equal than the current power. However, the power of the leading video card should remain unchanged, i.e. it can't be reduced.

Vlad has an infinite amount of money so he can buy any set of video cards. Help him determine which video cards he should buy such that after picking the leading video card and may be reducing some powers of others to make them work together he will get the maximum total value of video power.

Input

The first line of the input contains a single integer n (1 ≤ n ≤ 200 000) — the number of video cards in the shop.

The second line contains n integers a1a2, ..., an (1 ≤ ai ≤ 200 000) — powers of video cards.

Output

The only line of the output should contain one integer value — the maximum possible total power of video cards working together.

Examples
input
4
3 2 15 9
output
27
input
4
8 2 2 7
output
18
Note

In the first sample, it would be optimal to buy video cards with powers 315 and 9. The video card with power 3 should be chosen as the leading one and all other video cards will be compatible with it. Thus, the total power would be 3 + 15 + 9 = 27. If he buys all the video cards and pick the one with the power 2 as the leading, the powers of all other video cards should be reduced by 1, thus the total power would be 2 + 2 + 14 + 8 = 26, that is less than 27. Please note, that it's not allowed to reduce the power of the leading video card, i.e. one can't get the total power 3 + 1 + 15 + 9 = 28.

In the second sample, the optimal answer is to buy all video cards and pick the one with the power 2 as the leading. The video card with the power 7 needs it power to be reduced down to 6. The total power would be 8 + 2 + 2 + 6 = 18.

参考cf上的做法,确实不难啊

/*
一个前缀和pre[i]记录小于等于i的个数,对于每个数i,开始枚举这个数
的倍数j,寻找之间的可以为j的个数,并累加,最后求最大值。

*/
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
const int N = 200000;
int a[N+5],num[N*2+5],pre[N*2+5];
int main()
{
    ios_base::sync_with_stdio(0);
    int n;
    cin >> n;
    for(int i = 1;i <= n;i++)
    {
        cin >> a[i];
        num[a[i]]++;
    }

    for(int i = 1;i <= N*2;i++)
        pre[i] = pre[i-1] + num[i];

    ll ans = 0;
    for(int i = 1;i <= N;i++)
    {
        if(!num[i])
            continue;
        ll sum = 0;
        for(int j = i;j <= N;j += i)
        {
            ll temp = pre[j+i-1] - pre[j-1];
            if(temp < 0)
                temp = 0;
            sum += (ll)temp*j;
        }
        ans = max(ans,sum);
    }
    cout<<ans<<endl;
    return 0;
}


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值