Recipe
Time limit1sMemory limit1024 MB
Buy ingredients on some days, hold each in the fridge until a later day, cook it there if freshness stays at least L_i, and maximize the total of F_i minus elapsed days times C_j; print Impossible if day N can't be a cooking day.
- Level
Hard8 of 10
- Topics
- Dynamic programming, Greedy, Sorting, Two pointers
- Solved
- No attempts yet
Problem
Jaemin likes to cook. Over days he wants to develop several recipes. He develops one like this.
- Buy an ingredient at the market and put it in the fridge.
- Think up a recipe.
- Take the ingredient out of the fridge and cook it.
The market sells a new kind of ingredient every day. The ingredient sold on day has freshness . An ingredient kept in the fridge loses 1 freshness for each day that passes, so an ingredient bought on day and taken out on day has freshness . While an ingredient is still in the fridge, Jaemin does not buy another one before he cooks with it.
On day Jaemin has cooking skill . His skill keeps growing, so holds for , with . Taking an ingredient of freshness out of the fridge and cooking it with skill makes a dish of taste .
On a day he cooks he invites his friend Jaehyun. Jaehyun is very hygienic and wants the ingredient in the fridge to have freshness or more on day . If the ingredient in the fridge misses that standard, Jaemin cannot cook that day. Jaehyun's demand changes every day, so the standards for the days are given as .
After making a new dish, Jaemin goes to the market the next day, buys an ingredient and thinks up another recipe. While an ingredient sits in the fridge he may spend the day working out a recipe and put the cooking off, and he may also cook on the very day he buys. The fridge is empty on day 1, so he goes to the market and buys an ingredient, and on day he must cook and leave the fridge empty.
Find the largest possible sum of the tastes of the dishes Jaemin makes. If Jaehyun's fussy demands make it impossible to empty the fridge on day , print Impossible.
Input
The input has four lines.
The first line contains .
The second line contains separated by spaces.
The third line contains separated by spaces.
The fourth line contains separated by spaces.
Output
Print the largest possible sum of the tastes of the dishes Jaemin makes.
If the fridge cannot be emptied on day , print Impossible.