**Tutorial**

Tutorial is loading...

**Solution**

```
#include <bits/stdc++.h>
using namespace std;
int main() {
#ifdef _DEBUG
freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif
int t;
cin >> t;
for (int i = 0; i < t; ++i) {
int a, b, k;
cin >> a >> b >> k;
cout << (a - b) * 1ll * (k / 2) + a * (k & 1) << endl;
}
return 0;
}
```

**Tutorial**

Tutorial is loading...

**Solution**

```
#include <bits/stdc++.h>
using namespace std;
int main() {
#ifdef _DEBUG
freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; ++i) {
cin >> a[i];
}
int ans = 0;
for (int i = 1; i < n - 1; ++i) {
if (a[i] == 0 && a[i - 1] == 1 && a[i + 1] == 1) {
++ans;
a[i + 1] = 0;
}
}
cout << ans << endl;
return 0;
}
```

**Tutorial**

Tutorial is loading...

**Solution**

```
#include <bits/stdc++.h>
using namespace std;
const int MAX = 1e6;
int main() {
#ifdef _DEBUG
freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif
int n;
cin >> n;
vector<int> a(n);
vector<int> cnt(MAX + 1);
for (int i = 0; i < n; ++i) {
cin >> a[i];
++cnt[a[i]];
}
long long sum = accumulate(a.begin(), a.end(), 0ll);
vector<int> ans;
for (int i = 0; i < n; ++i) {
sum -= a[i];
--cnt[a[i]];
if (sum % 2 == 0 && sum / 2 <= MAX && cnt[sum / 2] > 0) {
ans.push_back(i);
}
sum += a[i];
++cnt[a[i]];
}
cout << ans.size() << endl;
for (auto it : ans) cout << it + 1 << " ";
cout << endl;
return 0;
}
```

**Tutorial**

Tutorial is loading...

**Solution**

```
#include <bits/stdc++.h>
using namespace std;
const int MAX = 200 * 1000 + 1;
int n, k;
vector<int> s, t;
vector<int> cnts(MAX);
bool can(int cnt) {
t.clear();
for (int i = 0; i < MAX; ++i) {
int need = min(cnts[i] / cnt, k - int(t.size()));
for (int j = 0; j < need; ++j) {
t.push_back(i);
}
}
return int(t.size()) == k;
}
int main() {
#ifdef _DEBUG
freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif
cin >> n >> k;
s = vector<int>(n);
for (int i = 0; i < n; ++i) {
cin >> s[i];
}
for (auto c : s) {
++cnts[c];
}
int l = 0, r = n;
while (r - l > 1) {
int mid = (l + r) >> 1;
if (can(mid)) {
l = mid;
} else {
r = mid;
}
}
if (!can(r)) can(l);
for (auto it : t) cout << it << " ";
cout << endl;
return 0;
}
```

**Tutorial**

Tutorial is loading...

**Solution**

```
#include <bits/stdc++.h>
using namespace std;
int main() {
#ifdef _DEBUG
freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif
int n;
cin >> n;
map<int, int> cnt;
for (int i = 0; i < n; ++i) {
int x;
cin >> x;
++cnt[x];
}
vector<int> cnts;
for (auto it : cnt) {
cnts.push_back(it.second);
}
sort(cnts.begin(), cnts.end());
int ans = 0;
for (int i = 1; i <= cnts.back(); ++i) {
int pos = int(cnts.size()) - 1;
int cur = i;
int res = cur;
while (cur % 2 == 0 && pos > 0) {
cur /= 2;
--pos;
if (cnts[pos] < cur) break;
res += cur;
}
ans = max(ans, res);
}
cout << ans << endl;
return 0;
}
```

1077F1 - Pictures with Kittens (easy version)

**Tutorial**

Tutorial is loading...

**Solution**

```
#include <bits/stdc++.h>
using namespace std;
const long long INF64 = 1e18;
int main() {
#ifdef _DEBUG
freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif
int n, k, x;
cin >> n >> k >> x;
vector<int> a(n);
for (int i = 0; i < n; ++i) {
cin >> a[i];
}
vector<vector<long long>> dp(n + 1, vector<long long>(x + 1, -INF64));
dp[0][x] = 0;
for (int i = 1; i <= n; ++i) {
for (int j = 0; j < x; ++j) {
for (int p = 1; p <= k; ++p) {
if (i - p < 0) break;
if (dp[i - p][j + 1] == -INF64) {
continue;
}
dp[i][j] = max(dp[i][j], dp[i - p][j + 1] + a[i - 1]);
}
}
}
long long ans = -INF64;
for (int i = n - k + 1; i <= n; ++i) {
ans = max(ans, *max_element(dp[i].begin(), dp[i].end()));
}
if (ans == -INF64) ans = -1;
cout << ans << endl;
return 0;
}
```

1077F2 - Pictures with Kittens (hard version)

**Tutorial**

Tutorial is loading...

**Solution**

```
#include <bits/stdc++.h>
using namespace std;
#define x first
#define y second
#define mp make_pair
typedef long long li;
typedef pair<li, li> pll;
const li INF64 = 1e18;
struct myQueue {
stack<pll> s1, s2;
int size() {
return s1.size() + s2.size();
}
bool isEmpty() {
return size() == 0;
}
long long getMax() {
if (isEmpty()) {
return -INF64;
}
if (!s1.empty() && !s2.empty()) {
return max(s1.top().y, s2.top().y);
}
if (!s1.empty()) {
return s1.top().y;
}
return s2.top().y;
}
void push(long long val) {
if (s2.empty()) {
s2.push(mp(val, val));
} else {
s2.push(mp(val, max(val, s2.top().y)));
}
}
void pop() {
if (s1.empty()) {
while (!s2.empty()) {
if (s1.empty()) {
s1.push(mp(s2.top().x, s2.top().x));
} else {
s1.push(mp(s2.top().x, max(s2.top().x, s1.top().y)));
}
s2.pop();
}
}
assert(!s1.empty());
s1.pop();
}
};
int n, k, x;
vector<int> a;
vector<myQueue> q;
vector<queue<int>> pos;
vector<vector<long long>> dp;
int main() {
#ifdef _DEBUG
freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif
cin >> n >> k >> x;
a = vector<int>(n);
for (int i = 0; i < n; ++i) {
cin >> a[i];
}
q = vector<myQueue>(x + 1);
pos = vector<queue<int>>(x + 1);
dp = vector<vector<long long>>(n + 1, vector<long long>(x + 1, -INF64));
dp[0][x] = 0;
pos[x].push(-1);
q[x].push(0ll);
for (int i = 1; i <= n; ++i) {
for (int j = 0; j <= x; ++j) {
while (!pos[j].empty() && pos[j].front() < i - k - 1) {
q[j].pop();
pos[j].pop();
}
}
for (int j = 0; j < x; ++j) {
if (!q[j + 1].isEmpty()) {
dp[i][j] = max(dp[i][j], q[j + 1].getMax() + a[i - 1]);
q[j].push(dp[i][j]);
pos[j].push(i - 1);
}
}
}
long long ans = -INF64;
for (int i = n - k + 1; i <= n; ++i) {
ans = max(ans, *max_element(dp[i].begin(), dp[i].end()));
}
if (ans == -INF64) ans = -1;
cout << ans << endl;
return 0;
}
```

In spite of being (of course) an overkill for problem F1 and F2, it's really interesting to point out that many problems of that kind are solvable with a linear programming approach. For example see this 45824522.

Of course, problem F2 has to be solved with a randomized-pivoting version of simplex algorithm (mine's is much simpler and shorter so it TLE's on Test 37).

The thing is that when you have this kind of conditions, the problem actually becomes solvable, because the matrix you have is totally unimodular (I didn't try to prove it this time, but I relied heavily on Problem D "Delight of a cat" from 2016-2017 NEERC Problemset).

Specially in online-contests on which you are allowed to copy-paste these long pieces of code, it's useful to be aware of this, because the actual writing part of the program is indeed straightforward.

Wow, that's quite cool and interesting!

Could someone explain to me the logic behind this if statement in problem C:

`if (sum % 2 == 0 && sum / 2 <= MAX && cnt[sum / 2] > 0)`

?.Let i be the index of the number being removed from the array. Hence, the new sum will be equal to sum(original sum)-arr[i]. Now, let arr[j] be the number for with the property holds true. Therefore, arr[j] = sum(new sum) — arr[j] => arr[j] = sum/2. As all the numbers in the array are integers with their maximum value being 10^6, if such an arr[j] exists, sum should be divisible by 2 and sum/2 should be less than or equal to 10^6. cnt[sum/2] simply tells whether such a number exists or not.

why do we have check sum/2 is less than 10^6?

we are looking for a number that equals to the rest of the array. so sum of all elements(including that number) sum = (our number plus rest of array). sum/2 = our number and sum/2 = sum of rest array. but our max value of number equals to 10^6 so that why we check sum/2 < 10^6. idk if it helps, but I tried

I am having trouble understanding tutorial logic,can anyone explain me problem 1077C — Good Array, Thanks

So, think in this manner. For every i, can it be a nice index or not ? How can we think to solve it for every 'i' in reasonable time ? Well, let's try to think what all do we need to calculate to answer that if 'i' is nice or not? So, we need to remove 'i'th element and then check whether the remaining sum is exactly twice of a single element in the array(after excluding ith element).

Finding the remaining sum is easy.(We can have a global sum variable and we will subtract a[i] from it).

Now, How to check whether this is twice of single element or not ? (hash table ?) Sure, we can have a cnt[] array which will say, cnt[5] = 3, that means three 5's are in array. (think of it as occurence/ frequency array).

Now, how this will help in finding the previous question ?

Ans: We have sum(remaining sum after excluding ith element), we can check that if(cnt[sum/2] >1 and sum%2==0). i.e.(sum should be even and there should be an element in array equal to sum/2). But there is a catch. It could happen that ith element itself = sum/2. So. we need to tackle that too. Sure, we can have nasty if else.

Tutorial presents an elegant way, which is:

`Find the sum(remaining sum)`

`subtract this element from cnt array(by decrementing it)`

`Now check for if(cnt[sum/2] >1 and sum%2==0)`

`again add this elememt to cnt array.`

`again add a[i] to sum.`

In case of some doubt, feel free to ask.

can you provide me solution in java?

https://codeforces.com/contest/1077/submission/45963342

Can someone please explain why this code gave WA for problem F1 ?

Another greedy approach for problem E:

If we have k distinct topics, let's call number of problems for j-th topic k[j] such that 0<=j<=k-1.

the following solution is built on an observation: if the optimal solution is to create X contests, then these X contests could be made from the X topics which have the largest number in array k.

So we need to sort array k to have the largest number in the beginning.

Then we need to know for every X (number of contests) what is the maximum number of problems we can use, let's call this number M.

We already know the answer to make 1 contest M = k[0]. Then for making 2 contests: M = min(M/2, k[1]). Generally M = min(M/2, k[X-1]). And finally for every X compute the answer as follows: ans= max( ans , M * ( 2^(X+1) — 1 ) ).

Great solution! Easier to understand than the tutorial one, thanks mate !

I solved the problem using exact same approach! However, I cannot understand the solution in tutorial :(

Can anyone please tell me why problem F2 cannot be solvable with Segment Tree(or any RMQ Data Structure) ?

Author was not saying it was unsolvable using segment tree, just that their method was easier to code

Another solution for D without Binary search using priority_queue, 45895914.

why are you doing f[i]/j? please explain.

To include every possible solution can get it from these Frequency, suppose the freq = 5, so i push 5, 2, 1, 1, 1 in priority queue if freq 1 is in the solution this guarantee that 2 and 5 in the solution so the best is to take this number 5 or k times in this case.

Ok, I think I got it. It should work this way. when freq 5 is chosen it says that we can have five copies of that number at max but since if k is greater, 2 will be chosen which implies that we can have 2 copies of that [number, number].

Same goes dynamically for every number. This way we'll have an array having numbers which will have maximum copies.

Your solution is small, clean and much easier to understand. Thanks!

Can you explain me your idea , please i want to learn

if we can divide the freq of any letter in small group of freq(with same letter), then if the smallest one is in solution the greater also in solution.

Can anyone tell me why am I getting wrong for problem D? https://codeforces.com/contest/1077/submission/45898888

Got it, thanks anyways

solved F2 using DP DnC

what is DP DnC ?

It's a dynamic programming optimization technique. The DnC stands for divide and conquer. You can learn more from here https://codeforces.com/blog/entry/8219

Thanks yellow_fellow! Your comment helped me to learn Divide & Conquer Optimization and solve this beautiful problem for Vova. ^_^

I am having trouble understanding the logic of 1077D — Cutting Out. Can someone please explain it and help me. Thanks!

NOTE: I was not able to solve it during the contest, but I was able to do it afterwards.

This is one type of binary_search problem. You will encounter many such problems. General binary_search which you might have studied(or will study) in the university is done over an array of numbers. In this kind of problems, you need to perform the binary_search over the entire solution space.

For e.g.: In this problem, you can consider the entire solution space as all numbers ranging from 1 to n (as you cannot have more than "n" copies).

So, step 1 of this problem is to find the maximum number of copies which can be cut out from the the given array. Let me call this value as

`val`

. How we are going to find`val`

, I will come back to that later. Let us move to Step 2Step 2Now, given that if we

wantto cut out`val`

copies from the given array, can we do that ?In order to answer this question we need to first construct a frequency array (In this we will store the frequency of all given numbers).

We need to construct an array of length

`k`

We iterate over all elements over the frequency array. First we check that whether the given`key`

in frequency array has`value`

more than`val`

. If it has a`value`

more than`val`

. Then this can be used in the construction of the array.But there is an interesting catch here. Since, duplicates are allowed in the given answer, we

mightbe able to use the same value again. So, we can use that value atmost`floor(value/val)`

number of times.So this is basically the

`can`

function being talked in the above tutorial.Now coming back to Step 1.We need to figure out what values of

`val`

are valid. Minimum Val = 0 Maximum Val = n If we do a binary_search over this, then we will be able to get the MAXIMUM possible value of`val`

.Another thing to note is binary_search can only be done when the array(solution space) is monotonically increasing. In this case, as mentioned in the tutorial, if you can create,

`val`

copies you can also create`val - 1`

copies.Here is my solution: http://codeforces.com/contest/1077/submission/45915120

Thanks!!!

Hello:Please help me honeysingh123 What do you recommend me?: Lately I have had problems when it comes to solving problems, it's like I'm at a stop, and I can not go any further, but I really like competitive programming, and I'd like to keep improving my skills, so I ask you what do you recommend me ?, Thanks in advance

I do not understand F2"each segment of the news feed of at least k consecutive pictures has at least one picture reposted by Vova;",if n=4,k=2 then we should choose one of 1,2 and one of 3 4.or choose one of 1 2 and one of 2 3 and one of 3 4?

I know it now.

Can u tell me ?? Unable to understand it properly

Can anyone please explain the problem statement of 1077B, I find it confusing especially that explanation of first test case and what does k pairwise district flat means? please illustrate with a test case. Thanks in advance!

As I understood pairwise distinct simply means that none of them should be counted (turned-off) twice or more.

Let's consider the first example

10

1 1 0 1 1 0 1 0 1 0

(Their index is 1-based)

As the statement, No. 3 and No. 6, No. 8 are disturbed (because both people next to them turned on)

To make sure there are nobody "disturbed", you must turn off some lights. One way : we can turn No. 2, 4, 5, 7, 9 off. This way, the testcase turns to

10

1 0 0 0 0 0 0 0 0 0

This was simple way to do the task : turn every light off which contributes making someone 'disturbed'

However, we can do better. note that if we turn No.2 off, we don't have to turn No.4 off. No. 3 is already in peace.

Note one more : if we turn No.7 off, we can make both No.6 and No.8 'not disturbed' by turning only one light off.

If we turn 2 and 7 off,

10

1 0 0 1 1 0 0 0 1 0

Nobody is disturbed.

If we turn 4 and 7 off

10

1 1 0 0 1 0 0 0 1 0

Nobody is disturbed.

We can't do better (i.e. task is impossible by turning 1 light off), so the answer is 2.

Hope this is helpful :)

In Q.1077-D I can't understand exactly how are we calculating value of 'val'.Can someone please explain it to me. I used a brute force approach for that and it's getting TLE.

Figured it out...thanks anyway

I can't understand, why in problem D solution this line is written ??

" if (!can(r)) can(l); " at the just end of binary search!

cause in binary search, the solution used l = mid; if l = 2, r = 3, mid = 2 and can(mid)=true, this will be infinity loop.

so you can see the loop ends where r-l>1 and after that r maybe an answer and l must be an answer.

but at condition above, we didn't check whether r can be an answer, if it can, this will be the real answer because r>=l

I am trying to submit solution for problem c in C but I am getting runtime error(46049311). Can any body let me know why this may be coming. It runs fine in my laptop.

Got the issues after compiling with clang instead of gcc.

I understand the solution of problem F1, but I don't know why my source code got 'Wrong Answer on test 45'(46061836).

Can anyone please explain me the reason? I've been thinking about this for two days, but I have not found the answer :(

Sorry but please explain to me why the dp[i][j] can equal to dp[p][j+1] + a[i-1] // why (j+1) and a[i-1] // Thank you so much

Vovuh please add this editorial to the dashboard . First I thought editorial is not published but when I searched it on google I found it .

[edit] comment was incorrect.

can anyone please explain why i am getting WA. my approach: iterate through X let it be x and iterate through n let it be i. Then find maximum elemment in dp[i-k-k-1: i-1] [x-1], add ara[i] with it and save in dp[i][x]. submission

C isnt just a max segment tree?

Can anyone help, why i am getting wrong answer at test case 12 in ques E. Here is my solution-https://codeforces.com/contest/1077/submission/54516795

I found the mistake.Here is accepted code-https://codeforces.com/contest/1077/submission/54522333

I can't understand solution of D please someone explain it

Getting TLE on using unordered_map, while AC on using map for problem E. Can anyone explain why??(I think unordered_map should be faste than map bcz its time complexity is O(1) for all operation.)

Link for TLE Solution

Link for AC Solution

unordered_map's time complexity is O(k) where k is the size of the bucket, usually it is small hence we consider it to be O(1). In your case, there must've been alot of collison hence getting tle. Ordered_map is LogN always.

why did i get runtime error in this code

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

using namespace std;

int main() {

}