When submitting a solution in C++, please select either C++14 (GCC 6-32) or C++17 (GCC 7-32) as your compiler. ×

beast_sid's blog

By beast_sid, history, 3 years ago, In English

why my code is giving runtime error in CSES dp-7th problem (book shop)??.Please help me. link to code- https://cses.fi/paste/31a10089630000cd20d9fe/

  • Vote: I like it
  • -8
  • Vote: I do not like it

| Write comment?
»
3 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Use int instead of long long. Using long long is causing memory limit to exceed causing RE.

»
3 years ago, # |
  Vote: I like it +9 Vote: I do not like it

You are exceeding the memory limit. If you look at your submission, you can see that you use more than the memory limit for that problem, which is 512MB.

In this case, this is easy to fix: your outer loop is over $$$i$$$, and at index $$$i$$$, you only access $$$DP[i]$$$ and $$$DP[i-1]$$$. Thus it is enough to maintain a $$$2 \times (x+1)$$$ array $$$DP'$$$, where at step $$$i$$$ you have $$$DP'[0] = DP[i - 1]$$$ and $$$DP'[1] = DP[i]$$$. When incrementing $$$i$$$ you can set $$$DP'[0][j] \leftarrow DP'[1][j]$$$.