Problem link - https://codeforces.com/contest/1374/problem/E2

Can anyone please help why am i getting wrong answer on test case 12 of problem E2.

My code — https://codeforces.com/contest/1374/submission/85427982 .

Logic - Initially I have taken all books which is interesting for both.Then if it is less than k then i have taken from remaining books which are in sorted order until the count of interesting books for both is equal to k.After that i have replaced the books interesting for both with books interesting to 1 only(2 books added for each 1 book interesting to both removed).Afterwards I have just added more books to make the total count m.I am not being able to figure out what is wrong in this approach. Plz. help.

When you are directly selecting books interesting for both. I think first you should chose books like E1 and say now you have x books and condition of k is satisfied. Now if xm then remove books which only Alice liked and which only Bob liked and add which both liked. Correct me if I am wrong