#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; typedef long long ll; struct Edge { int from; int to; int value; bool valid; Edge(int from = -1, int to = -1, int value = -1){ this->from = from; this->to = to; this->value = value; this->valid = true; } }; struct Node { int id; int value; Node(int id = -1, int value = -1){ this->id = id; this->value = value; } bool operator >(const Node &e) const{ return value > e.value; } }; vector edgeList; vector nodeList; map pickList; int n, m; priority_queue, greater > pque; void updateNodeValue(){ pque = priority_queue, greater >(); for(int i = 0; i < n; i++){ Edge le = edgeList[(i+n-1)%n]; Edge re = edgeList[(i+n)%n]; int value = 0; if(le.valid){ value += le.value; } if(re.valid){ value += re.value; } if(!pickList[i]){ pque.push(Node(i, value)); } } } int main(){ cin >> n >> m; int w; for(int i = 0; i < n; i++){ cin >> w; Edge edge(i, (i+1)%n, w); edgeList.push_back(edge); Node node; nodeList.push_back(node); } for(int i = 0; i < n-m; i++){ updateNodeValue(); Node node = pque.top(); pque.pop(); edgeList[(node.id+n-1)%n].valid = false; edgeList[(node.id+n)%n].valid = false; pickList[node.id] = true; } int answer = 0; while(!pque.empty()){ Node node = pque.top(); pque.pop(); //fprintf(stderr,"node %d: value %d\n", node.id, node.value); } for(int i = 0; i < n; i++){ Edge edge = edgeList[i]; if(edge.valid){ answer += edge.value; } } cout << answer << endl; return 0; }