using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static int Index_finder_left(int[] napici, int N)
        {
            for (int Kleft = 0; Kleft < N; Kleft++)
            {
                if (napici[Kleft] != 0)
                {
                    return Kleft;
                }
            }
            return 0;
        }
        static int Index_finder_right(int[] napici, int N)
        {
            for (int Kright = N - 1; Kright >= 0; Kright--)
            {
                if (napici[Kright] != 0)
                {
                    return Kright;
                }
            }
            return N - 1;
        }
        static bool IsABiggerThanB(int[] napici, int N)
        {
            if (napici[Index_finder_left(napici, N)] > napici[Index_finder_right(napici, N)])
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        static void NajNajNapitak(int[] napici, int N, int K)
        {
            for (int PK = 0; PK < K - 1; PK++)
            {
                if (IsABiggerThanB(napici, N) == true)
                {
                    if (napici[Index_finder_left(napici, N)] - napici[Index_finder_right(napici, N)] == 0)
                    {
                        PK++;
                    }
                    napici[Index_finder_left(napici, N)] -= napici[Index_finder_right(napici, N)];
                    napici[Index_finder_right(napici, N)] = 0;
                }
                else
                {
                    if (napici[Index_finder_right(napici, N)] - napici[Index_finder_left(napici, N)] == 0)
                    {
                        PK++;
                    }
                    napici[Index_finder_right(napici, N)] -= napici[Index_finder_left(napici, N)];
                    napici[Index_finder_left(napici, N)] = 0;
                }
            }
        }
        static void Ispisivanje(int[] napici, int N)
        {
            int dbg = 0;
            for (int P = 0; P < N; P++)
            {
                if (napici[P] != 0)
                {
                    Console.WriteLine(napici[P]);
                    dbg = 1;
                    break;
                }
            }
            if (dbg == 0) { Console.WriteLine(0); }
        }
        static void Main(string[] args)
        {
            int N, K;
            string[] NK = Console.ReadLine().Split(' ');
            N = int.Parse(NK[0]);
            K = int.Parse(NK[1]);
            int[] napici = Array.ConvertAll(Console.ReadLine().Split(' '), int.Parse);
            NajNajNapitak(napici, N, K);
            Ispisivanje(napici, N);
        }
    }
}