It’s horribly written.
You mean his code of my code or my answer ?
I mean the question, I remember when I first read it, I wasn’t able to understand it until my roommate explained it to me, lol, I thought I suck at understanding, but turns out the author, apparently admin2 sucks at writing.
ohh XD
I disagree,
I found it good enough to understand though.
How can we subtract k from all those values greater than k, to ans, because that can happen only if there are even no of those values, otherwise how will we pair them up?
Sample Input:
**3**
**2 1**
**1 2**
2 1
2 2
3 1
2 3 2
Sample Output:
**3**
2
5
It is easy question.
Let’s do first output…
You have 3 test cases
Array of two integers and your k is and will be 1
You go to your array next line and you choose two a’s in this case a1 =1 and a2=2
Min(a1,a2)=1
So no decrement will happen here
And your maximum sum for what you get left in your array here in this case is 3.
Did you understand or you need the rest test cases explain.
what would be the answer for
5 5
4 4 6 6 9
There will be repetion put that in mind .
We choose a1and a2 random but here we will not chhose them again only this time let say a1=4and a2 =4 min(a1,a2) <k =5 so no decrement In this case and summation 29
Then do other choice for the a’s and that other case and other summation answer.
I believe you need to paires for getting the maximum summation ,paired for the a’s.
Like (a1=4,a2=6) (a1=6,a2=6) (a1=6,a2=9)
But the maximum summation (5,5) or (5,8) both min =5 so no more decremntal will happen here but we have two max sum 4 4 5 5 9
Or 4 4 5 6 8
Idont know if they do more decrement after the first decrement .
Seems like you misunderstood the statement.
if(j<a[n-2])
cout << ans+a[n-1]-(a[n-2]-j) << “\n”;
else if((j%2)!=(a[n-2]%2))
cout << ans+a[n-1]-1 << “\n”;
else
cout << ans+a[n-1] << “\n”;
I’m not getting this
4 4 6 6 9
ans = 4 +4+5+5+5
Make it
0 0 1 1 4
Now
0 0 0 0 4
ans+=4
Have you read my answer instead of code ?
I wrote everything there.
Click on
"How does this work ? "
Read my answer 2-3 times and you’ll get it.
Which portion of code takes care of case 2? And why that parity code?
And my code breaks at this test case:
10 5
3 3 3 4 5 7 8 9 43 76
Can you tell what is the answer for it, showing manually the pairs we will pick up?
Edit: edited the solution to give optimum sum
10 5
3 3 3 4 5 7 8 9 43 76
Reduce (7,43) as far as possible
3 3 3 4 5 5 8 9 41 76
Next Reduce (8, 41)
3 3 3 4 5 5 5 9 38 76
Next Reduce (9, 38)
3 3 3 4 5 5 5 5 34 76
Finally Reduce (34, 76)
3 3 3 4 5 5 5 5 5 47
No more reductions possible.
Final sum is 85.
But AC code gives 85
Corrected it
#20 chars
Thanks everyone! Got it!
That “parity check” and “else” takes care of case 2
I didn’t understand your code,but the choices for the paires could be random because in the questions he said if you do multiple choice, but then he want the maximum sum for farest decrement you could have in the given array?!