A. Array with Odd Sum
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
You are given an array a consisting of n integers.

In one move, you can choose two indices 1≤i,j≤n such that i≠j and set ai:=aj. You can perform such moves any number of times (possibly, zero). You can choose different indices in different operations. The operation := is the operation of assignment (i.e. you choose i and j and replace ai with aj).

Your task is to say if it is possible to obtain an array with an odd (not divisible by 2) sum of elements.

You have to answer t independent test cases.

Input
The first line of the input contains one integer t (1≤t≤2000) — the number of test cases.

The next 2t lines describe test cases. The first line of the test case contains one integer n (1≤n≤2000) — the number of elements in a. The second line of the test case contains n integers a1,a2,…,an (1≤ai≤2000), where ai is the i-th element of a.

It is guaranteed that the sum of n over all test cases does not exceed 2000 (∑n≤2000).

Output
For each test case, print the answer on it — “YES” (without quotes) if it is possible to obtain the array with an odd sum of elements, and “NO” otherwise.

``````#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define me(a,x) memset(a,x,sizeof a)
#define pb(a) push_back(a)
#define pa pair<int,int>
#define fi first
#define se second

int main()
{
int t;cin>>t;
while(t--){

int n;cin>>n;
int odd=0,even=0,sum=0;
for(int i=0,x;i<n;i++){
cin>>x,sum+=x;
if(x&1) odd++;
else even++;
}
puts((odd&&even || sum&1)?"YES":"NO");
}
return 0;
}

``````

time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
Mishka wants to buy some food in the nearby shop. Initially, he has s burles on his card.

Mishka can perform the following operation any number of times (possibly, zero): choose some positive integer number 1≤x≤s, buy food that costs exactly x burles and obtain ⌊x10⌋ burles as a cashback (in other words, Mishka spends x burles and obtains ⌊x10⌋ back). The operation ⌊ab⌋ means a divided by b rounded down.

It is guaranteed that you can always buy some food that costs x for any possible value of x.

Your task is to say the maximum number of burles Mishka can spend if he buys food optimally.

For example, if Mishka has s=19 burles then the maximum number of burles he can spend is 21. Firstly, he can spend x=10 burles, obtain 1 burle as a cashback. Now he has s=10 burles, so can spend x=10 burles, obtain 1 burle as a cashback and spend it too.

You have to answer t independent test cases.

Input
The first line of the input contains one integer t (1≤t≤104) — the number of test cases.

The next t lines describe test cases. Each test case is given on a separate line and consists of one integer s (1≤s≤109) — the number of burles Mishka initially has.

Output
For each test case print the answer on it — the maximum number of burles Mishka can spend if he buys food optimally.

``````#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define me(a,x) memset(a,x,sizeof a)
#define pb(a) push_back(a)
#define pa pair<int,int>
#define fi first
#define se second
int main()
{

int t;cin>>t;
while(t--){

int n;cin>>n;
int sum=n;
while(n>=10){

int r=n/10;
sum+=r;
n=n%10+r;
}
cout<<sum<<endl;

}

return 0;
}
``````

C. Yet Another Walking Robot
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
There is a robot on a coordinate plane. Initially, the robot is located at the point (0,0). Its path is described as a string s of length n consisting of characters ‘L’, ‘R’, ‘U’, ‘D’.

Each of these characters corresponds to some move:

‘L’ (left): means that the robot moves from the point (x,y) to the point (x−1,y);
‘R’ (right): means that the robot moves from the point (x,y) to the point (x+1,y);
‘U’ (up): means that the robot moves from the point (x,y) to the point (x,y+1);
‘D’ (down): means that the robot moves from the point (x,y) to the point (x,y−1).
The company that created this robot asked you to optimize the path of the robot somehow. To do this, you can remove any non-empty substring of the path. But this company doesn’t want their customers to notice the change in the robot behavior. It means that if before the optimization the robot ended its path at the point (xe,ye), then after optimization (i.e. removing some single substring from s) the robot also ends its path at the point (xe,ye).

This optimization is a low-budget project so you need to remove the shortest possible non-empty substring to optimize the robot’s path such that the endpoint of his path doesn’t change. It is possible that you can’t optimize the path. Also, it is possible that after the optimization the target path is an empty string (i.e. deleted substring is the whole string s).

Recall that the substring of s is such string that can be obtained from s by removing some amount of characters (possibly, zero) from the prefix and some amount of characters (possibly, zero) from the suffix. For example, the substrings of “LURLLR” are “LU”, “LR”, “LURLLR”, “URL”, but not “RR” and “UL”.

You have to answer t independent test cases.

Input
The first line of the input contains one integer t (1≤t≤1000) — the number of test cases.

The next 2t lines describe test cases. Each test case is given on two lines. The first line of the test case contains one integer n (1≤n≤2⋅105) — the length of the robot’s path. The second line of the test case contains one string s consisting of n characters ‘L’, ‘R’, ‘U’, ‘D’ — the robot’s path.

It is guaranteed that the sum of n over all test cases does not exceed 2⋅105 (∑n≤2⋅105).

Output
For each test case, print the answer on it. If you cannot remove such non-empty substring that the endpoint of the robot’s path doesn’t change, print -1. Otherwise, print two integers l and r such that 1≤l≤r≤n — endpoints of the substring you remove. The value r−l+1 should be minimum possible. If there are several answers, print any of them.

``````#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define me(a,x) memset(a,x,sizeof a)
#define pb(a) push_back(a)
#define pa pair<int,int>
#define fi first
#define se second
bool cmp(pa a,pa b){

return a.se-a.fi<b.se-b.fi;
}
int main()
{

int t;cin>>t;
while(t--){

int n;cin>>n;
string s;cin>>s;
map<pa,int> mp;
int flag=0;
int x=0,y=0;
mp[pa(x,y)]=1;
vector<pa> v;
for(int i=0;i<n;i++){

if(s[i]=='L') x--;
if(s[i]=='R') x++;
if(s[i]=='U') y++;
if(s[i]=='D') y--;
if(mp[pa(x,y)]){

v.pb(pa(mp[pa(x,y)],i+1));

}
mp[pa(x,y)]=i+2;
}
if(v.empty()) {puts("-1");continue;}
sort(v.begin(),v.end(),cmp);
cout<<v[0].fi<<" "<<v[0].se<<endl;

}

return 0;
}
``````

D. Fight with Monsters
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
There are n monsters standing in a row numbered from 1 to n. The i-th monster has hi health points (hp). You have your attack power equal to a hp and your opponent has his attack power equal to b hp.

You and your opponent are fighting these monsters. Firstly, you and your opponent go to the first monster and fight it till his death, then you and your opponent go the second monster and fight it till his death, and so on. A monster is considered dead if its hp is less than or equal to 0.

The fight with a monster happens in turns.

You hit the monster by a hp. If it is dead after your hit, you gain one point and you both proceed to the next monster.
Your opponent hits the monster by b hp. If it is dead after his hit, nobody gains a point and you both proceed to the next monster.
You have some secret technique to force your opponent to skip his turn. You can use this technique at most k times in total (for example, if there are two monsters and k=4, then you can use the technique 2 times on the first monster and 1 time on the second monster, but not 2 times on the first monster and 3 times on the second monster).

Your task is to determine the maximum number of points you can gain if you use the secret technique optimally.

Input
The first line of the input contains four integers n,a,b and k (1≤n≤2⋅105,1≤a,b,k≤109) — the number of monsters, your attack power, the opponent’s attack power and the number of times you can use the secret technique.

The second line of the input contains n integers h1,h2,…,hn (1≤hi≤109), where hi is the health points of the i-th monster.

Output
Print one integer — the maximum number of points you can gain if you use the secret technique optimally.

``````#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define me(a,x) memset(a,x,sizeof a)
#define pb(a) push_back(a)
#define pa pair<int,int>
#define fi first
#define se second
bool cmp(pa a,pa b){

return a.se-a.fi<b.se-b.fi;
}
int main()
{

int n,a,b,k;
cin>>n>>a>>b>>k;
int c=a+b;
vector<int> q(n+10);
for(int i=1;i<=n;i++){
cin>>q[i];
q[i]%=c;
if(!q[i]) q[i]=c;
}
sort(q.begin()+1,q.begin()+n+1);
int ans=0;
for(int i=1;i<=n;i++){
// cout<<i<<" "<<q[i]<<endl;
if(q[i]<=a)  ans++;
else
{
int r=q[i]/a;
if(q[i]%a==0) r--;
if(r<=k) k-=r,ans++;
else break;
}
}
cout<<ans<<endl;

return 0;
}
``````

E1. String Coloring (easy version)
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
This is an easy version of the problem. The actual problems are different, but the easy version is almost a subtask of the hard version. Note that the constraints and the output format are different.

You are given a string s consisting of n lowercase Latin letters.

You have to color all its characters one of the two colors (each character to exactly one color, the same letters can be colored the same or different colors, i.e. you can choose exactly one color for each index in s).

After coloring, you can swap any two neighboring characters of the string that are colored different colors. You can perform such an operation arbitrary (possibly, zero) number of times.

The goal is to make the string sorted, i.e. all characters should be in alphabetical order.

Your task is to say if it is possible to color the given string so that after coloring it can become sorted by some sequence of swaps. Note that you have to restore only coloring, not the sequence of swaps.

Input
The first line of the input contains one integer n (1≤n≤200) — the length of s.

The second line of the input contains the string s consisting of exactly n lowercase Latin letters.

Output
If it is impossible to color the given string so that after coloring it can become sorted by some sequence of swaps, print “NO” (without quotes) in the first line.

Otherwise, print “YES” in the first line and any correct coloring in the second line (the coloring is the string consisting of n characters, the i-th character should be ‘0’ if the i-th character is colored the first color and ‘1’ otherwise).

``````#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define me(a,x) memset(a,x,sizeof a)
#define pb(a) push_back(a)
#define pa pair<int,int>
#define fi first
#define se second
int main()
{
int n;cin>>n;
string s;cin>>s;
vector<int> v(n,0);
int l='a';
for(int i=0;i<n;i++)
if(s[i]>=l) l=s[i],v[i]=1;

l='a';
for(int i=0;i<n;i++)
if(!v[i] && s[i]>=l) l=s[i],v[i]=2;
for(int i=0;i<n;i++){
if(!v[i]){
return puts("NO"),0;
}
}
puts("YES");
for(int i=0;i<n;i++){
cout<<v[i]-1;
}

return 0;
}

``````

E2

``````#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define me(a,x) memset(a,x,sizeof a)
#define pb(a) push_back(a)
#define pa pair<int,int>
#define fi first
#define se second
int main()
{
int n;cin>>n;
string s;cin>>s;
vector<int> v(n,0);
int ans=1,num=0;
while(num<n){

int l='a';
for(int i=0;i<n;i++)
if(!v[i] && s[i]>=l) l=s[i],v[i]=ans,num++;
ans++;
}

cout<<ans-1<<endl;
for(int i=0;i<n;i++) cout<<v[i]<<" ";

return 0;
}

``````