Thanks for participating!

Idea: flamestorm

**Tutorial**

Tutorial is loading...

**Solution**

```
#include <bits/stdc++.h>
using namespace std;
const int MAX = 200007;
const int MOD = 1000000007;
void solve() {
string s;
cin >> s;
if (s[0] != 'y' && s[0] != 'Y') {cout << "NO\n";}
else if (s[1] != 'e' && s[1] != 'E') {cout << "NO\n";}
else if (s[2] != 's' && s[2] != 'S') {cout << "NO\n";}
else {cout << "YES\n";}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int tt; cin >> tt; for (int i = 1; i <= tt; i++) {solve();}
// solve();
}
```

Idea: flamestorm

**Tutorial**

Tutorial is loading...

**Solution**

```
#include <bits/stdc++.h>
using namespace std;
const int MAX = 200007;
const int MOD = 1000000007;
void solve() {
int n;
cin >> n;
string s;
cin >> s;
bool vis[26] = {};
int res = 0;
for (char c : s) {
if (!vis[c - 'A']) {res += 2; vis[c - 'A'] = true;}
else {res++;}
}
cout << res << '\n';
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int tt; cin >> tt; for (int i = 1; i <= tt; i++) {solve();}
// solve();
}
```

Idea: mesanu

**Tutorial**

Tutorial is loading...

**Solution**

```
#include <bits/stdc++.h>
using namespace std;
void solve()
{
int n;
cin >> n;
int a[n];
for(int i = 0; i < n; i++)
{
cin >> a[i];
}
for(int i = 0; i < n; i++)
{
int b;
cin >> b;
if(b == 0)
{
continue;
}
string now;
cin >> now;
for(int j = 0; j < b; j++)
{
if(now[j] == 'U'){a[i]--;}
else if(now[j] == 'D'){a[i]++;}
if(a[i] < 0){a[i]+=10;}
if(a[i] > 9){a[i]-=10;}
}
}
for(int i = 0; i < n; i++)
{
cout << a[i] << " ";
}
cout << endl;
}
int main(){
int t;
cin>> t;
while(t--)
{
solve();
}
return 0;
}
```

Idea: MikeMirzayanov

**Tutorial**

Tutorial is loading...

**Solution**

```
#include <bits/stdc++.h>
using namespace std;
const int MAX = 200007;
const int MOD = 1000000007;
void solve() {
int n;
cin >> n;
string s[n];
map<string, bool> mp;
for (int i = 0; i < n; i++) {
cin >> s[i];
mp[s[i]] = true;
}
for (int i = 0; i < n; i++) {
bool ok = false;
for (int j = 1; j < s[i].length(); j++) {
string pref = s[i].substr(0, j), suff = s[i].substr(j, s[i].length() - j);
if (mp[pref] && mp[suff]) {ok = true;}
}
cout << ok;
}
cout << '\n';
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int tt; cin >> tt; for (int i = 1; i <= tt; i++) {solve();}
// solve();
}
```

Idea: mesanu

**Tutorial**

Tutorial is loading...

**Solution**

```
#include <bits/stdc++.h>
using namespace std;
void solve()
{
int n;
cin >> n;
int a[n][n];
for(int i = 0; i < n; i++)
{
for(int j = 0; j < n; j++)
{
char c;
cin >> c;
a[i][j] = c-'0';
}
}
int ans = 0;
for(int i = 0; i < (n+1)/2; i++)
{
for(int j = 0; j < n/2; j++)
{
int nowi = i, nowj = j;
int oldnowj = nowj;
int sum = a[nowi][nowj];
nowj = n-nowi-1;
nowi = oldnowj;
sum+=a[nowi][nowj];
oldnowj = nowj;
nowj = n-nowi-1;
nowi = oldnowj;
sum+=a[nowi][nowj];
oldnowj = nowj;
nowj = n-nowi-1;
nowi = oldnowj;
sum+=a[nowi][nowj];
ans+=min(sum, 4-sum);
}
}
cout << ans << endl;
}
int main(){
int t;
cin>> t;
while(t--)
{
solve();
}
return 0;
}
```

1703F - Очередная задача про пары, удовлетворяющие неравенству

Idea: flamestorm

**Tutorial**

Tutorial is loading...

**Solution**

```
#include <bits/stdc++.h>
using namespace std;
const int MAX = 200007;
const int MOD = 1000000007;
void solve() {
int n;
cin >> n;
int a[n + 1];
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
long long res = 0;
vector<int> v;
for (int i = 1; i <= n; i++) {
if (a[i] >= i) {continue;}
res += (long long)(lower_bound(v.begin(), v.end(), a[i]) - v.begin());
v.push_back(i);
}
cout << res << '\n';
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int tt; cin >> tt; for (int i = 1; i <= tt; i++) {solve();}
// solve();
}
```

1703G - Хороший ключ, плохой ключ

Idea: mesanu

**Tutorial**

Tutorial is loading...

**Solution**

```
#include <bits/stdc++.h>
using namespace std;
void solve()
{
int n, k;
cin >> n >> k;
int a[n];
for(int i = 0; i < n; i++)
{
cin >> a[i];
}
long long ans = 0;
long long sum = 0;
for(int i = -1; i < n; i++)
{
long long now = sum;
for(int j = i+1; j < min(n, i+32); j++)
{
int copy = a[j];
copy>>=j-i;
now+=copy;
}
ans = max(ans, now);
if(i+1 != n)
{
sum+=a[i+1]-k;
}
}
cout << ans << endl;
}
int main(){
int t;
cin >> t;
while(t--)
{
solve();
}
return 0;
}
```

Quickly

most satisfying G solution : state : dp[i][j] means at ith element we use exactly j bad keys. trans : either we have to use bad key at ith place or it already been use before.

Hey I would like to request a clarification: why do you report answer as the maximum of dp[i][j] over all values of i and j? should it not be just maximum value of dp[n][j] over all values of j?

I also want to ask this question，did you get the answer?

Because of their definition "dp[i][j] means at ith element we use

exactlyj bad keys". Note that we can definitely use more than 33 bad keys, but it won't matter because after 33 bad keys all elements to the right will be 0. So, considering dp[i][j] for i < n is equivalent of handling all cases where a[k] becomes 0 for all k > i (note ai >= 0). If the definition was "at most j bad keys", then we could just answer max over j of dp[n][j], but the transition would be different.Sorry for the late reply, for the answer Think like you brute forcing and you can use j keys, now after using j keys, You dont know which previous step give you the best answer. So new question arises at any stage did we know the best answer ? the answer is "NO" , for any ith step we are checking complete ith step. That's the simplified reason why we are checking complete i * j for the last answer.

Hey! I was solving this problem using DP and I,ve written almost the same code.

Please have alook at my submission:262108989.

I dont understant the reason it is giving WA of tc4.

I am new to DP and I have been trying to debug since all night. please help

Thanks for fast tutorial

Amazing contest !! ..E is tricky one!

That's why I Aced F instead of E.

E is full implementation based

F is solvable with prefix sums in $$$O(n)$$$.

For each $$$i$$$ count the number of elements, where $$$j \leq i$$$ and $$$a_j<j$$$ ($$$pref_i$$$). And for each $$$i$$$ where $$$a_i<i$$$ just add $$$pref_{a_i-1}$$$ to the answer.

Here is my solution 163953789.

You beat me by 1 second, gonna say the same thing.

can you please explain a bit more?

the prefix sum approach isn't really clear to me?

For every element we can store how many element we have encountered before it, that have v[i] < i

Now for every element we can find whether we have it's value in range and whether we can find element so that we can satisfy the 2nd part in the condition i.e. i < v[j], other conditions are already satisfied

My Submission

Can you plz tell me why your code is giving correct ans for the case when a[i]-1 becomes -1. Beacause that should give a garbage value

My bad, thanks for noticing. Probably it is $$$0$$$ in this case.

Now the code is correct.

Can G be solved by DP ?

Yes I solved G using DP. I used the fact that applying the bad operation 30 times would make all elements 0 since log(1e9) is approximately 30.

Here's my solution: https://codeforces.com/contest/1703/submission/163933460

I will say that it was a good contest. Thanks for the quick tutorial

Hey! Can anyone clarify why my solution is showing TLE on Test Case 10 (Problem D), my approach is similar as that mentioned in the Editorial. Thanks! 163871330

Your problem lies here I suppose:

SpoilerWhen you wrote

`ans = ans + '1'`

what the code did was calculate`ans + '1'`

thenassign that resulted stringto`ans`

, which made this operation $$$O(N)$$$. Use`ans += '1'`

next time.Bonus: Here's your very same code accepted with those changes.

You just need to speed up the I/O. Just add this line (before defining t int your main funtion).

`ios::sync_with_stdio(false);cin.tie(nullptr);`

After that, tabulate the code correctly and you'll get AC.No it didn't

Fast IO does not solve every problem.But your tabulation is not correct.

But who asked.

I think problem F can be done in

`O(n)`

time.As mentioned, we only need to consider indices

`i`

such that`a_i < i`

. Call such indices "good". We can make a prefix array such that the`i`

th element is the number of good indices less than or equal to`i`

.Now consider all possible pairs

`(i,j)`

that work for a fixed good index`j`

. The number of possible values for`i`

is the number of good indices less than`a_j`

, which is stored within the prefix array. The answer can then be found by summing across all good indices`j`

.The prefix array can be created in 1 pass, so that takes

`O(n)`

time. The summing across all good indices can also be done in 1 pass, so this is`O(n)`

time again.Code for this can be found in my submission

Edit: Apparently got sniped lmao

Can you plz tell me why your code is giving correct ans for the case when a[i]-1 becomes -1. Beacause that should give a garbage value

I specifically excluded it in the if condition to handle that bug

`if a[i] < i+1 and a[i] > 0:`

i have done the same. What do you mean by "sniped" ?

Someone else said a similar thing while I was typing my message

Problem G was a good one.

Yes

For the problem D we can use

`unordered_set<string> cache`

to store the strings. Then for each string, bruteforrce divide the strings in two and check whether both of these strings exist in the`cache`

or not. Simpleto be honest though, you should be very cautious on using hash-based data structures in codeforces. Some people are masters of hacking and can easily find hundreds, even thousands of hash collisions.

Thats true... I think one way we can avoid is by making 2 or three hashes differently and storing in each of them. That way the chance of getting same hashes reduces significantly

This code of mine for question D(double strings) is giving TLE on test 3 and I don't know why. Please help me where is the problem?

Your solution is $$$O(n^2)$$$.

Can F be solved by Fenwick tree??

Yes. 163916062

Yes

can u explain please why the index in segment tree is ai ( which could be 1e9 ) insted of indexes of original array

any solution which can count pairs where $$$i < a_j$$$ where $$$a_i < i, a_j < j$$$ in all $$$i$$$ and $$$j$$$ is a valid solution. therefore Segment Trees and Fenwick Trees are valid solutions, so are prefix sums.

Yes. I have done it using segment tree. here is my solution 163925757

Honestly, F was too easy with Segment Trees (or Fenwick Trees, those two have identical purposes). I think any person with some experience of common segment tree problems would easily solve it.

BS enters the room*

Prefix Sum enters next and slap both on the face*

it can be easily solved with pbds with a few lines of code

yes, very easy code.

Let's see in the hacking phase if people learned to use the map instead of the unordered_map. beethoven97 I summon you!

why this solution 163935126 giving TLE ? Anybody please help

because memset function fills the DP using its size --> O(N), here it is N = 1e5 so your dp gets filled for all test cases which can be upto 1e4, so

~1e9operations per second are performed.use thiswhy my dp solution doesn't work for G https://codeforces.com/contest/1703/submission/163929073

For the testcase:

your code gives a negative answer. The dp needs to be able to go from dp[i-1][31] to dp[i][31] without decreasing it by k.

Here is a little trick. We know, that answer always $$$0$$$ or bigger (because we can just open chests for free). That means answer is not only $$$max(dp[n][i]) \; for \; all \; i < 32$$$, but $$$max(dp[i][j]) \; for \; all \; i < n \; all \; j < 32$$$

Thank you for the very good contest, all the problems are enjoyable

I learn some new thing from problem G

Can anyone tell why using vector in this solution for problem E gives wrong answer? Replacing vector with plain array makes it accepted. Submission link

Look at the case when x = 0, you are accessing sum[x-1] or sum[-1]

You're right. I guess it was luck that it got accepted with c array.

someone hacked my D (163901816) can anyone provide me test case ?

Wait for system testing

DP solution for G. Lang: PyPy3-64 ____________________________________________________________________________________

Could help me G problem?i use dp to slove it ,but i cant fine my problem.thank u very much

problem G

TAT...my english is poor ,but i try to share my idea

j:the number of good key

pw(2,x)=2**x

when we open the i th box ,if we use j good keys so we use (i-j) bad keys.but pw(2,33)>1e9,so i think the number of good key is lower than 33.

if the number of bad key is upper than 33,we can see it as 1e14.

so when we open some box,if we use good keys ,we got

a[i]/(pw[(i-j)] -mcostand we use bad keys ,we got

a[i]/(pw[(i-j)] -mso DP[i][j] is mean open i boxes use j good keys.

Dp[i][j]=Dp[i-1][j]+ bad keys or DP[i][j]=DP[i-1][j-1] + good keys

so whats wrong with me TAT

Suppose that

nandkare very large And the optimal operations are always use bad keys. In this case obviously we use more than 32 bad keys but your solution don't considers this and gives the wrong answer.To fix it : for calculating answer iterate over all dp values and get maximum instead of only

dp[n — 1](because if we do this we consider above case):My submission : 163915709

For first 30 minutes i think in E we need to make matrix by add 1. Later i eays accepted this Problem

G is quite hard, other problems are great. Hope the rating will change soon so well-perform participants could get to the next rank!

Thanks for the fast editorial

Another F approach. We can create

pwherep[i] = (a[i] < i), after that create a prefix sum on that array and iterate overaand do the following:p[i] = 1, otherwise continuea[i] >= 2, otherwise continueprefsum[a[i] — 1]to the answerSo why do we need

a[i] >= 2statement? Ifa[i] < 2, then there is no way to satisfy this inequality.a[i] >= 0,i >= 1, thereforea[i] < i < 1 < jcan't be satisfied. Check out my implementation if you are interested 164005949. What do you think about this one?P.S Well, just find out some guy already told about the same approach...

Simple recursive DP solution for G here.

`map<string, bool>`

? Why not just use a`set<string>`

?Can someone tell me why this dp solution is getting WA, doesn't make any sense to me? https://codeforces.com/contest/1703/submission/163945776

You also have to update your answer while calculating dp so as to counter cases when you use more than 30 bad moves. One such example will be, 60 chests containing 2 coins and lets say k = 1e9.

You are saying that i have to take the max of the whole matrix?

Not really the whole matrix, max of $$$dp[i][30]$$$ over all chests $$$i$$$ + your current logic will be sufficient.

Video Solutions of complete problemset.

F can be solved in linear time. submission

Ratings did not seem to update. Anyone else having this problem?

Yes... Not updated

Just checked again. Ratings are updated now.

Can anyone please tell why am I getting Memory Limit Exceeded in Problem E? I am just using a 2D Array. Link to my submission :- https://codeforces.com/contest/1703/submission/163908463

Hello sir, There are two layers of recurrence in your code, so your code's time complexity is $$$O(n^2)$$$, and when $$$n = 200000$$$,you will get TLE.

I solved this problem with set, and my time complexity is $$$o(n \log n)$$$.

This is my code:

Although there are two lawers of recurrence in my code, but $$$s[i].size \le 8$$$, so my code doesn't go TLE.

Hey, Thanks for the reply but I am asking about Problem E, Mirror Grid. The solution that you have explained is Problem D, Double Strings.

You are creating new 2D array every test case.

Thank you so much for replying. Can you tell what should be approach for this problem?

To be honest I didn't solve this problem and now I don't have time now because the contest starts in 2 minutes.

To fix your problem (MLE) you can create 2D array before first test case. (before

`while (t > 0)`

line)Because in the editorial they are also creating a 2D array for each test case.

Then I don't know. Maybe I was wrong.

I solved F in O(n) Here is my solution: 163897273

Good solution for problem G!

Can someone suggest more problems similar to G?(like DP states like this)

Deleted

In G, reason why we have to iterate over all dp array instead of only go through dp[n][0-32] is that during the transferation of dp array, once the second dimension hit the boundary, we would lose one stratey, which is using the bad key. Because we were supposed to do dp[i][33]=dp[i-1][33-1]+a[i]>>32(or 33?im not sure), but the boundary force us to only do dp[i][32]=dp[i-1][31]+a[i]>>32(?)-k, which is using the good key, taking all the cost painfully. so u see how the bad key strategy is lost during the process, besides iterating all dp array, we can also do dp[i][j]=dp[i-1][j-1] when a[i]>>j==0.

why do i get tle on this method?

but the authors approach works fine?

is there a formula shows where cell i,j maps to when rotate 90, 180, 270 degrees in problem E?

// By: TESFAMICHAEL TAFERE

## include <bits/stdc++.h>

using namespace std;

int main() { int n;

}