I sometimes get confused when to use greedy and when to use DP. I have this problem — https://codeforces.net/contest/455/problem/A where I thought my solution which is greedy would be correct but it went wrong on a 100 element array when submitted. I don't seem to find any mistake or failing test case for my solution. Can someone help? My solution: https://codeforces.net/contest/455/submission/299737860
Auto comment: topic has been updated by canvas_melody (previous revision, new revision, compare).
Auto comment: topic has been updated by canvas_melody (previous revision, new revision, compare).
When a={1, 2, 3, 4} your code prints 5, although it's also possible to first select 4 and delete 3 and then select 2 and delete 1, for a sum of 6.
The main reason is that your code sometimes thinks choosing an element will result in too many elements being deleted, without considering the case that they already are.
I'm not sure there is a greedy solution, but it can definitely be solved with DP.
As for when you should use DP, it's generally used when:
You'll get better at recognizing DP problems with time (even if you solve problems you already knew were DP), because you'll understand these patterns better, so keep practicing!