2018-3-16 codeforces edu 39

1,A. Partition
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
You are given a sequence a consisting of n integers. You may partition this sequence into two sequences b and c in such a way that every element belongs exactly to one of these sequences.

Let B be the sum of elements belonging to b, and C be the sum of elements belonging to c (if some of these sequences is empty, then its sum is 0). What is the maximum possible value of B - C?

Input
The first line contains one integer n (1 ≤ n ≤ 100) — the number of elements in a.

The second line contains n integers a1, a2, …, an ( - 100 ≤ ai ≤ 100) — the elements of sequence a.

Output
Print the maximum possible value of B - C, where B is the sum of elements of sequence b, and C is the sum of elements of sequence c.

Examples
inputCopy
3
1 -2 0
output
3
inputCopy
6
16 23 16 15 42 8
output
120
Note
In the first example we may choose b = {1, 0}, c = { - 2}. Then B = 1, C =  - 2, B - C = 3.

In the second example we choose b = {16, 23, 16, 15, 42, 8}, c = {} (an empty sequence). Then B = 120, C = 0, B - C = 120.

#include<iostream>
using namespace std;
int main(){
    ios::sync_with_stdio(false);cin.tie(0);
    int n;cin>>n;
    int sum1=0,sum2=0;
    for(int i=0;i<n;i++){
        int a;cin>>a;
        if(a>=0)sum1+=a;
        else sum2+=a; 
    }
    cout<<sum1-sum2<<endl;
}

2,B. Weird Subtraction Process
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
You have two variables a and b. Consider the following sequence of actions performed with these variables:

If a = 0 or b = 0, end the process. Otherwise, go to step 2;
If a ≥ 2·b, then set the value of a to a - 2·b, and repeat step 1. Otherwise, go to step 3;
If b ≥ 2·a, then set the value of b to b - 2·a, and repeat step 1. Otherwise, end the process.
Initially the values of a and b are positive integers, and so the process will be finite.

You have to determine the values of a and b after the process ends.

Input
The only line of the input contains two integers n and m (1 ≤ n, m ≤ 1018). n is the initial value of variable a, and m is the initial value of variable b.

Output
Print two integers — the values of a and b after the end of the process.

Examples
inputCopy
12 5
output
0 1
inputCopy
31 12
output
7 12
Note
Explanations to the samples:

a = 12, b = 5 a = 2, b = 5 a = 2, b = 1 a = 0, b = 1;
a = 31, b = 12 a = 7, b = 12.

#include<iostream>
using namespace std;
int main(){
    ios::sync_with_stdio(false);cin.tie(0);
    long long int a,b;
    cin>>a>>b;
    while(a!=0&&b!=0){
        if(a>=2*b)a=a%(2*b);
        else {
            if(b>=2*a)b=b%(2*a);
            else break;
        }
    }
    cout<<a<<" "<<b<<endl;
}

3,C. String Transformation
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
You are given a string s consisting of |s| small english letters.

In one move you can replace any character of this string to the next character in alphabetical order (a will be replaced with b, s will be replaced with t, etc.). You cannot replace letter z with any other letter.

Your target is to make some number of moves (not necessary minimal) to get string abcdefghijklmnopqrstuvwxyz (english alphabet) as a subsequence. Subsequence of the string is the string that is obtained by deleting characters at some positions. You need to print the string that will be obtained from the given string and will be contain english alphabet as a subsequence or say that it is impossible.

Input
The only one line of the input consisting of the string s consisting of |s| (1 ≤ |s| ≤ 105) small english letters.

Output
If you can get a string that can be obtained from the given string and will contain english alphabet as a subsequence, print it. Otherwise print «-1» (without quotes).

Examples
inputCopy
aacceeggiikkmmooqqssuuwwyy
output
abcdefghijklmnopqrstuvwxyz
inputCopy
thereisnoanswer
output
-1

#include<iostream>
#include<string>
using namespace std;
int main(){
    ios::sync_with_stdio(false);cin.tie(0);
    string str;cin>>str;
    int temp=0,len=str.length();
    for(int i=0;i<len;i++){
        if(str[i]-'a'<=temp){
            temp++;str[i]='a'+temp-1;
        }
        if(temp==26)break;
    }
    if(temp==26)cout<<str<<endl;
    else cout<<-1<<endl;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值