---
id: 319
lang: en
translation_of: index.md
source_sha256: 284d88c6ced89a819f668b497883796b90a38f0fe77d49f23ef00dc64f209a0b
title: "BZOJ2288: Birthday Gift — Solution"
description: "Merge runs of same-sign numbers, then keep taking the smallest absolute value off a heap and merging it with its neighbors in a linked list."
tags: ["OI","Heap","Linked List"]
translated: 2026-11-27
translator: Claude Opus 5.5
translator_model: claude-opus-5-5
translation_notes:
  - "方案数 / 方案套数 (literally “number of schemes / scheme sets”) rendered literally as “scheme count” / “number of schemes”."
  - "The formulas $prv[pos]!=0且nxt[pos]!=tot+1$ and $i、i-1、i+1$ contain Chinese (且 “and”, the list comma 、) outside \\text{…}; kept unchanged because TeX must stay identical."
---

## Approach

Actually, this problem is just asking for the maximum sum of at most $M$ (possibly mutually overlapping) subsequences of a sequence. We can merge all the consecutive positive numbers and negative numbers in the sequence together: positives merge with positives, negatives merge with negatives. Then I add all the positive numbers to the answer, put them into a min-heap keyed by their absolute values, and count them, while at the same time building a doubly linked list structure. After that, we can use $cnt>m$ as the loop condition and keep taking elements out of the queue to process them.

<!--more-->

When processing, we first check which case we are in. If this point is not at an edge position (that is, position $prv[pos]!=0且nxt[pos]!=tot+1$), then we directly subtract its absolute value. If it is a positive number at an edge, then just subtract it from the answer! But if it is unlucky and does not meet the conditions above, that is, it is a negative number at an edge, then we just decrease the scheme count by one and move on to the next iteration.

After that, we merge. We merge elements $i、i-1、i+1$ all together and then put the result into the heap, because if we pick it again later, that means we have picked elements $i-1,i+1$. After merging, a problem comes up: if we delete the predecessor and successor, then in some iteration of the big loop, a deleted element will show up and cannot be processed. We just need to prepare a $vis[]$, and at the start of the loop keep filtering until one meets the condition — that is, stop when it exists.

Finally, when the number of schemes is less than the limit $M$, the loop stops and we output the answer. (Of course, there is also the case where the scheme count is already small before processing, and wouldn't that be even better.)

## Code

```cpp
// CH1812.cpp
#include <iostream>
#include <queue>
#include <cstdio>
#include <cstring>
#include <cmath>
#define ll long long
using namespace std;
const int maxn = 100200;
ll arr[maxn], prv[maxn], nxt[maxn], N, M, merged[maxn];
bool vis[maxn];
struct node
{
    ll pid, val;
    node() {}
    node(ll pd, ll v) { pid = pd, val = v; }
    bool operator<(const node &nd) const { return abs(val) > abs(nd.val); }
};
priority_queue<node> nds;
void del(ll p) { vis[p] = false, prv[nxt[p]] = prv[p], nxt[prv[p]] = nxt[p]; }
int main()
{
    scanf("%lld%lld", &N, &M);
    for (int i = 1; i <= N; i++)
        scanf("%lld", &arr[i]);
    ll tot = 1;
    // merge them to one.
    for (int i = 1; i <= N; i++)
        if (arr[i] * merged[tot] >= 0)
            merged[tot] += arr[i];
        else
            merged[++tot] = arr[i];
    ll ans = 0, cnt = 0;
    for (int i = 1; i <= tot; i++)
    {
        nds.push(node((ll)i, merged[i]));
        if (merged[i] > 0)
            ans += merged[i], cnt++;
        prv[i] = i - 1, nxt[i] = i + 1;
    }
    memset(vis, true, sizeof(vis));
    while (cnt > M)
    {
        cnt--;
        node curt = nds.top();
        while (!vis[curt.pid])
            nds.pop(), curt = nds.top();
        nds.pop();
        int i = curt.pid;
        if (prv[i] && nxt[i] != tot + 1)
            ans -= abs(merged[i]);
        else if (merged[i] > 0)
            ans -= merged[i];
        else
        {
            cnt++;
            continue;
        }
        merged[i] += merged[prv[i]] + merged[nxt[i]];
        del(prv[i]), del(nxt[i]);
        curt.val = merged[i], nds.push(curt);
    }
    printf("%lld", ans);
    return 0;
}
```
