import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintStream; import java.io.PrintWriter; import java.util.Arrays; import java.util.Iterator; import java.util.PriorityQueue; public class Main_yukicoder324 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); Printer pr = new Printer(System.out); n = sc.nextInt(); int m = sc.nextInt(); w = new int[n]; not = new boolean[n]; PriorityQueue pq = new PriorityQueue(); for (int i = 0; i < n; i++) { w[i] = sc.nextInt(); } for (int i = 0; i < n; i++) { pq.add(new Pair(i)); } int cnt = 0; while (cnt < n - m) { Pair e = pq.remove(); int l = (e.id - 1 + n) % n; int r = (e.id + 1 + n) % n; if ((e.le != not[l]) || (e.re != not[r])) { continue; } not[e.id] = true; cnt++; if (!e.le) { pq.add(new Pair(l)); } if (!e.re) { pq.add(new Pair(r)); } } int ret = 0; for (int i = 0; i < n; i++) { if (!not[i]) { int l = (i - 1 + n) % n; int r = (i + 1 + n) % n; if (!not[l]) { ret += w[l]; } if (!not[r]) { ret += w[i]; } } } pr.println(ret / 2); pr.close(); sc.close(); } private static int n; private static int[] w; private static boolean[] not; private static class Pair implements Comparable { int id; int sum; boolean le; boolean re; Pair(int id) { this.id = id; int l = (id - 1 + n) % n; int r = (id + 1 + n) % n; sum = (not[l] ? 0 : w[l]) + (not[r] ? 0 : w[id]); le = not[l]; re = not[r]; } @Override public int compareTo(Pair o) { return Integer.compare(this.sum, o.sum); } } @SuppressWarnings("unused") private static class Scanner { BufferedReader br; Iterator it; Scanner (InputStream in) { br = new BufferedReader(new InputStreamReader(in)); } String next() throws RuntimeException { try { if (it == null || !it.hasNext()) { it = Arrays.asList(br.readLine().split(" ")).iterator(); } return it.next(); } catch (IOException e) { throw new IllegalStateException(); } } int nextInt() throws RuntimeException { return Integer.parseInt(next()); } long nextLong() throws RuntimeException { return Long.parseLong(next()); } float nextFloat() throws RuntimeException { return Float.parseFloat(next()); } double nextDouble() throws RuntimeException { return Double.parseDouble(next()); } void close() { try { br.close(); } catch (IOException e) { // throw new IllegalStateException(); } } } private static class Printer extends PrintWriter { Printer(PrintStream out) { super(out); } } }