Alice is at the Mad Hatter's tea party! There is a long sheet cake made up of n sections…
- User
Alice is at the Mad Hatter's tea party! There is a long sheet cake made up of n sections with tastiness values a1,a2,…,an . There are m creatures at the tea party, excluding Alice.
Alice will cut the cake into m+1 pieces. Formally, she will partition the cake into m+1 subarrays, where each subarray consists of some number of adjacent sections. The tastiness of a piece is the sum of tastiness of its sections. Afterwards, she will divvy these m+1 pieces up among the m creatures and herself (her piece can be empty). However, each of the m creatures will only be happy when the tastiness of its piece is v or more.
Alice wants to make sure every creature is happy. Limited by this condition, she also wants to maximize the tastiness of her own piece. Can you help Alice find the maximum tastiness her piece can have? If there is no way to make sure every creature is happy, output −1 .
i wanted to solve this prolem using dp
where pos curr postion of dp then meaning how mcuh moster are satisfy and nichi niche = 0 means i didn't start my segment niche = 1 means i have start but not finished niche = 2means i have fined to take my segment and i need to now just give me monster and check if those are valid or not
int solve(int pos, int take, int nichi) {
int ans = -INF; if (nichi == 1) { ans = solve(pos + 1, 1) + A[pos]; int nxtpos = upper_bound(csum, csum + n + 1, csum[pos - 1] + v) - csum; ans = max(ans, solve(nxtpos, take - 1, 2)); } if (nichi == 2) { int nxtpos = upper_bound(csum, csum + n + 1, csum[pos - 1] + v) - csum; ans = max(ans, solve(nxtpos, take - 1, 2)); } if (nichi == 0) { }}
- Assistant