We finally had a codeforces round that went smooth and rated. :) Past two rounds that I participated in, both had something going wrong with it, and ended up unrated.

**DIV-2 Problem A; Ilya and Bank Account:**

You are given an integer N, and you are allowed to drop its last digit, or second last digit, to create an integer with greater value. If N is greater then 0, dropping allowed digits can not increase its value. If it is smaller then 0, dropping one of the allowed digit will give you the largest possible integer.

1 2 3 4 5 6 7 8 9 10 11 12 13 |
n = raw_input() n1 = n[:-1] n2 = n[:-2]+n[-1] n = int(n) n1 = int(n1) n2 = int(n2) if n<0: print max(n1, n2) else: print n |

**DIV2 Problem B; Ilya and Queries:**

First, iterate over the array, calculating positions where s[i]==s[i+1]. Lets call this new array dp. Then dp[0] = 0. For i=1 to s.size()-1, dp[i] = dp[i-1] if s[i]!=s[i-1], or else dp[i] = dp[i-1]+1. Now to find out count of all the integers ‘i’ between [l, r), such that s[i] = s[i+1], we can simply use dp[r]-d[l].

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
int main() { string s; cin>>s; vector<int> dp(s.size(), 0); for(int i=1;i<s.size();i++) { if(s[i]==s[i-1]) { dp[i] = dp[i-1]+1; } else { dp[i] = dp[i-1]; } } int m; cin>>m; for(int i=0;i<m;i++) { int a, b; cin>>a>>b; a--; b--; cout<<dp[b]-dp[a]<<endl; } return 0; } |

**DIV2 Problem C; Ilya and Matrix:**

We want to place all of the 4^n integers in the matrix in such a way, that the beauty of matrix is maximised. Beauty of matrix depends on the highest integer within it, plus the beauty of matrices that we get once we divide given matrix into four square matrices. One of these matrices will get our original highest integer that existed in the bigger matrix this smaller matrix is part of. In order to maximise beauty of the remaining three, it makes sense to place the next three highest integers out of our original 4^n integers in them. In the next step, we will have 16 sub matrices, and in order to maximise beauty of each, it would make sense that all of those 16 have the 16 highest integers from within the 4^n integers. And so on. Thus we iterate over sorted array of our 4^n integers, n times, each time adding first 4^i (where i is iteration number) integers from the array into our result.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
int main() { int n4; cin>>n4; int p = 1; int n=0; while(p!=n4) { n++; p *= 4; } vector<long long> v; for(int i=0;i<n4;i++) { long long a; cin>>a; v.push_back(a); } sort(v.rbegin(), v.rend()); long long s = 0; while(n>=0) { int dd = (int)pow(4.0, (double)n); for(int i=0;i<dd;i++) { s+=v[i]; } n--; } cout<<s<<endl; return 0; } |

**DIV2 Problem D; Ilya and Roads:**

This was a nice dynamic programming problem. Though I couldn’t figure correct recurrence for it. If someone has a simple explanation for it, please comment.

**Conclusion**:

I got a +140 in rating. Yay!