# | User | Rating |
---|---|---|

1 | tourist | 3697 |

2 | Benq | 3583 |

3 | Petr | 3522 |

4 | ecnerwala | 3467 |

5 | Radewoosh | 3466 |

6 | maroonrk | 3369 |

7 | Um_nik | 3358 |

8 | jiangly | 3330 |

9 | Miracle03 | 3314 |

10 | scott_wu | 3313 |

# | User | Contrib. |
---|---|---|

1 | 1-gon | 214 |

2 | Errichto | 189 |

3 | awoo | 188 |

4 | rng_58 | 187 |

5 | SecondThread | 186 |

6 | Um_nik | 179 |

7 | Ashishgup | 177 |

8 | maroonrk | 172 |

8 | vovuh | 172 |

8 | antontrygubO_o | 172 |

↑

↓

Codeforces (c) Copyright 2010-2021 Mike Mirzayanov

The only programming contests Web 2.0 platform

Server time: Apr/16/2021 21:30:54 (j3).

Desktop version, switch to mobile version.

Supported by

User lists

Name |
---|

It's pretty much Knapsack DP.

I will mention the dynamics:

$$$dp_{i,j,0}\ =maximum \ amount \ paid\ for\ first\ i\ elements\ with\ sweetness\ <=j\ and\ operation\ is not\ used.$$$

$$$dp_{i,j,1}\ =maximum \ amount \ paid\ for\ first\ i\ elements\ with\ sweetness\ <=j\ and\ operation\ is\ used.$$$

$$$Transitions\ are\ similar\ to\ Knapsack\ DP.$$$

Your answer will be $$$max(dp[n][m][0]\ ,\ dp[n][m][1])$$$

My solution: https://ideone.com/NgqsT4

Brother please can you clear my doubt I use 3 dp states dp[n][m][flag] The code passed the test cases but when I was using 2 dp states dp[n][m] and was updating the value for flag . Then it was failing on few cases why is so???

This is my previous code:- https://pastebin.com/E1hqm56H

This is new code:- https://pastebin.com/xEqn2Uq8

The problem is with the statement

, since we don't know if flag was true or false, in the returned state,so it may lead to overcount.

Thanks I understood my mistake

I did in recursive way with Memoization...I also made choices in that recursion part..! Click Here — My Submission — This problem is pretty similar to knapsack one just here,there is one more case.. You can check the submission I did with comments

Bro I can't see your sub plz share it through paste bin or ideone

Brother please can you clear my doubt I use 3 dp states dp[n][m][flag] The code passed the test cases but when I was using 2 dp states dp[n][m] and was updating the value for flag . Then it was failing on few cases why is so???

This is my previous code:- https://pastebin.com/E1hqm56H

This is new code:- https://pastebin.com/xEqn2Uq8

Using memoization. Code