1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
| #include <bits/stdc++.h>
using namespace std;
constexpr int MAXN = 2e5 + 5;
int n, m, a[MAXN], cnt, ans;
struct ListNode { ListNode() = default; bool operator<(const ListNode& other) const { return this->data == other.data ? this->id < other.id : this->data < other.data; } ListNode *prev = NULL, *next = NULL, *cur = NULL; int data = 0, id = -1; }; map<int, bool> deleted; priority_queue<ListNode> q;
struct CycleList { CycleList() = default;
ListNode* insert(int data, int id) { auto newNode = new ListNode(); newNode->data = data, newNode->id = id, newNode->cur = newNode; if (head == NULL) head = tail = newNode; newNode->prev = tail, tail->next = newNode, tail = newNode; tail->next = head, head->prev = tail; return newNode; }
ListNode* erase(ListNode* cur) { auto prev = cur->prev, next = cur->next; int data = prev->data + next->data - cur->data; deleted[prev->id] = deleted[cur->id] = deleted[next->id] = true; auto newNode = new ListNode(); newNode->data = data, newNode->id = cnt++, newNode->cur = newNode; prev->prev->next = newNode, newNode->prev = prev->prev; next->next->prev = newNode, newNode->next = next->next; return newNode; }
ListNode *head = NULL, *tail = NULL; } lst;
void calcA(int tot = 0) { while (!q.empty() && tot < m) { auto cur = q.top(); q.pop(); if (deleted[cur.id]) continue; ans += cur.data, tot++; auto newNode = lst.erase(cur.cur); q.push(*newNode); } }
int main() { scanf("%d %d", &n, &m); if (n / 2 < m) return printf("Error!\n"), 0; for (int i = 0; i < n; i++) { scanf("%d", a + i); auto newNode = i == n - 1 ? lst.insert(a[i], cnt++) : lst.insert(a[i], cnt++); q.push(*newNode); } calcA(); printf("%d\n", ans); return 0; }
|