Language: C++

#define ANSWER
#ifndef ANSWER
#include "mylist.hpp"
#endif

template <typename T> 
void mylist<T>::input(){
    mystd::InputArray<T>(list);
}

template <typename T> 
void mylist<T>::sort(){
    std::sort(list.begin(), list.end());
};

template <typename T> 
std::vector<size_t> mylist<T>::argsort(){
    std::vector<size_t> index(list.size());
    std::iota(index.begin(), index.end(), 0);

    std::sort(index.begin(), index.end(), [this](size_t i1, size_t i2) {
        return this->list[i1] < this->list[i2];
    });

    return index;
};

template <typename T> 
int mylist<T>::find(T key){
    typename std::vector<T>::iterator iter = std::find(list.begin(), list.end(), key);
    return std::distance(list.begin(), iter);
}

template <typename T> 
T mylist<T>::pop(size_t i){
    T ans = list[i];
    list.erase(list.begin()+i);
    return ans;
}

template <typename T> 
void mylist<T>::push(T value){
    list.push_back(value);
}

template <typename T> 
std::string mylist<T>::toString(){
    std::string string(list.begin(), list.end());
    return string;
}

template <typename T> 
void mylist<T>::fromString(std::string string){
    mylist<char> S;
    std::copy(string.begin(), string.end(), std::back_inserter(list));
}

template <typename T> 
void mylist<T>::fromAlphabets(std::string string){
    std::transform(string.begin(), string.end(), string.begin(), ::tolower);
    for (size_t i = 0; i < string.size(); i++)
    {
        typename std::vector<char>::iterator iter = std::find(alphabets.begin(), alphabets.end(), string[i]);
        list.push_back(std::distance(alphabets.begin(), iter));
    }            
}

template <typename T> 
std::string mylist<T>::toAlphabets(bool upper){
    std::string alp;
    for (auto &&i : list)
    {
        alp.push_back(alphabets[i - std::floor(i / alphabets.size()) * alphabets.size()]);
    }
    if (upper)
    {
        std::transform(alp.begin(), alp.end(), alp.begin(), ::toupper);
    }
    
    return alp;     
}

template <typename T> 
template <typename V> 
void mylist<T>::add(V value){
    for (auto &&i : list)
    {
        i+=value;
    }
}

template <typename T> 
void mylist<T>::range(){
    std::iota(list.begin(), list.end(), 0);
}

template <typename T> 
void mylist<T>::range(long long length){
    list.resize(length);
    std::iota(list.begin(), list.end(), 0);
}

template <typename T> 
void mylist<T>::range(long long start,long long end){
    list.resize(end-start);
    std::iota(list.begin(), list.end(), start);
}

#include <algorithm>
#include <vector>
#include <iostream>
#include <numeric>
#include <cmath>


template <typename T> 
class mylist {
    private:
        std::vector<char> alphabets = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'}; 
    public:
        mylist(long long i)
        {
            list.resize(i);
        }
        mylist()
        {}
        void input();

        std::vector<T> list;
        void sort();
        std::vector<T> sorted();
        std::vector<size_t> argsort();
        int find(T key);

        T pop(size_t i);
        void push(T value);
        
        std::string toString();
        void fromString(std::string string);
        void fromAlphabets(std::string string);
        std::string toAlphabets(bool upper);
        template <typename V>
        void add(V value);

        void range();
        void range(long long length);
        void range(long long start,long long end);
};
#ifndef ANSWER
#include "mymath.hpp"
#endif

template <typename T>
T mystd::factorical(T a)
{
    T ans = 1;
    for (size_t i = 0; i < a; i++)
    {
        ans *= i + 1;
    }
    return ans;
}

template <typename T>
T mystd::LCM(T a, T b)
{
    return a * b / GCD(a,b);
}

template <typename T>
T mystd::GCD(T a, T b)
{
    if (a < b)
    {
        T tmp = a;
        a = b;
        b = tmp;
    }

    T r = a % b;
    while (r != 0)
    {
        a = b;
        b = r;
        r = a % b;
    }
    return b;
}

template <typename T>
T mystd::LCM(std::vector<T> a)
{
    if (a.size() < 1)
    {
        return 1;
    }
    else
    {
        T lcm = LCM(a[0], a[1]);
        for (size_t i = 1; i < a.size() - 1; i++)
        {
            T lcm_temp = LCM(lcm, a[i + 1]);
            if (lcm < lcm_temp){
                lcm = lcm_temp;
            }
        }
        return lcm;
    }
}

template <typename T>
T mystd::LCM(mylist<T> a)
{
    if (a.list.size() < 1)
    {
        return 1;
    }
    else
    {
        T lcm = LCM(a.list[0], a.list[1]);
        for (size_t i = 1; i < a.list.size() - 1; i++)
        {
            T lcm_temp = LCM(lcm, a.list[i + 1]);
            if (lcm < lcm_temp){
                lcm = lcm_temp;
            }
        }
        return lcm;
    }
}

template <typename T>
T mystd::GCD(std::vector<T> a)
{
    if (a.list.size() < 1)
    {
        return 1;
    }
    else
    {
        T gcd = GCD(a[0], a[1]);
        for (size_t i = 1; i < a.size() - 1; i++)
        {
            T gcd_temp = GCD(gcd, a[i + 1]);
            if (gcd < gcd_temp){
                gcd = gcd_temp;
            }
        }
        return gcd;
    }
}

template <typename T>
T mystd::GCD(mylist<T> a)
{
    if (a.list.size() < 1)
    {
        return 1;
    }
    else
    {
        T gcd = GCD(a.list[0], a.list[1]);
        for (size_t i = 1; i < a.list.size() - 1; i++)
        {
            T gcd_temp = GCD(gcd, a.list[i + 1]);
            if (gcd < gcd_temp){
                gcd = gcd_temp;
            }
        }
        return gcd;
    }
}
namespace mystd
{
template <typename T>
T factorical(T a);
template <typename T>
T LCM(T a, T b);
template <typename T>
T GCD(T a, T b);
template <typename T>
T LCM(std::vector<T> A);
template <typename T>
T GCD(std::vector<T> A);
template <typename T>
T LCM(mylist<T> A);
template <typename T>
T GCD(mylist<T> A);
} // namespace mystd
#ifndef ANSWER
#include "mystd.hpp"
#endif

template<typename T>
bool mystd::Input(int number,...){
    va_list args;
    va_start(args, number);
    for (long long i = 0; i < number; i++)
    {
        std::cin >> *(va_arg(args, T*));
    }
    return true;
}

template<typename T>
void mystd::Print(int number,...){
    va_list args;
    va_start(args, number);
    for (long long i = 0; i < number; i++)
    {
        if(i!=0){
            std::cout << " ";
        }
        std::cout << *(va_arg(args, T*));
    }
    std::cout << std::endl;;
}

template<typename T>
void mystd::PrintArray(std::vector<T> array,const std::string sep){
    for (long long i = 0; i < array.size(); i++)
    {
        if(i!=0){
            std::cout << sep;
        }
        std::cout << array[i];
    }
    std::cout << std::endl;;
}

template<typename T>
bool mystd::InputArray(std::vector<T> &array){
    for (size_t i = 0; i < array.size(); i++)
    {
        std::cin >> array[i];
    }
    return true;
}

bool mystd::InputArrays(long long number,...){
    va_list args;
    va_start(args, number);
    std::vector<std::vector<long long>*> arrays(number);
    for (auto &i : arrays)
    {
        i = va_arg(args, std::vector<long long>*);
    }
    long long length = arrays[0]->size();

    for (long long j=0;j<length;j++)
    {
        for (auto &&i : arrays)
        {
            std::cin >> (*i)[j];
        }
    }
    return true;
}


long long mystd::min(std::vector<long long> array){
    long long ans = array[0];
    for (long long &i : array)
    {
        if(i < ans){
            ans = i;
        }
    }
    return ans;
}


long long mystd::max(std::vector<long long> array){
    long long ans = array[0];
    for (long long &i : array)
    {
        if(i > ans){
            ans = i;
        }
    }
    return ans;
}

#include <iostream>
#include <vector>
#include <cstdarg>
#include <cmath>
#include <string>

namespace mystd
{
    template<typename T>
    static bool Input(int number,...);
    template<typename T>
    static bool InputArray(std::vector<T> &array);
    static bool InputArrays(long long number,...);
    static bool InputMatrix(std::vector<std::vector<long long>> array);
    static long long min(std::vector<long long> array);
    static long long max(std::vector<long long> array);
    template<typename T>
    static void Print(int number,...);
    template<typename T>
    static void PrintArray(std::vector<T> array,const std::string sep);
};
// #include <algorithm>
// #include <vector>
// #include <iostream>
// #include <numeric>
// #include <string>
 
// class mystring {
//     public:
//         std::string str;
//         void sort(); 
//         std::string sorted();
//         std::string argsort();
//         int find(std::string key){
//             typename std::string::iterator iter = std::find(string.begin(), string.end(), key);
//             return std::distance(string.begin(), iter);
//         };
// };

// #include <algorithm>
// #include <vector>
// #include <iostream>
// #include <numeric>
// #include <string>
 
// class mystring {
//     public:
//         std::string str;
//         void sort(); 
//         std::string sorted();
//         std::string argsort();
//         int find(std::string key){
//             typename std::string::iterator iter = std::find(string.begin(), string.end(), key);
//             return std::distance(string.begin(), iter);
//         };
// };

#ifndef ANSWER
#include "../../library/mystd.cpp"
#include "../../library/mylist.cpp"
#include "../../library/mymath.cpp"
#endif

typedef long long ll;
typedef std::vector<long long> vll;
typedef long double ld;
typedef std::vector<long double> vld;

using namespace mystd;

void Main(){
    ll N,M;
    Input<ll>(2,&N,&M);
    mylist<ll> A(N);
    A.input();
    A.sort();

    int state = 0;
    ll i = 0;
    ll j = 0;
    ll ans = 0;
    for (size_t x = 0; x < M; x++)
    {
        ans += A.list[N-i-1] + A.list[N-j-1];
        if (state == 0)
        {
            i++;
            state ++;
        }
        else if(state==1)
        {
            i--;
            j++;
            state ++;
        }
        else if(state==2)
        {
            i++;
            state = 0;
        }
    }

    Print<ll>(1,&ans);
}

int main(int argc, char const *argv[])
{
    Main();
    return 0;
}

Compiler output:
a.cc:7:12: error: expected initializer before '<' token
    7 | void mylist<T>::input(){
      |            ^
a.cc:12:12: error: expected initializer before '<' token
   12 | void mylist<T>::sort(){
      |            ^
a.cc:17:6: error: 'vector' in namespace 'std' does not name a template type
   17 | std::vector<size_t> mylist<T>::argsort(){
      |      ^~~~~~
a.cc:1:1: note: 'std::vector' is defined in header '<vector>'; this is probably fixable by adding '#include <vector>'
  +++ |+#include <vector>
    1 | #define ANSWER
a.cc:29:11: error: expected initializer before '<' token
   29 | int mylist<T>::find(T key){
      |           ^
a.cc:35:9: error: expected initializer before '<' token
   35 | T mylist<T>::pop(size_t i){
      |         ^
a.cc:42:12: error: expected initializer before '<' token
   42 | void mylist<T>::push(T value){
      |            ^
a.cc:47:6: error: 'string' in namespace 'std' does not name a type
   47 | std::string mylist<T>::toString(){
      |      ^~~~~~
a.cc:1:1: note: 'std::string' is defined in header '<string>'; this is probably fixable by adding '#include <string>'
  +++ |+#include <string>
    1 | #define ANSWER
a.cc:53:12: error: expected initializer before '<' token
   53 | void mylist<T>::fromString(std::string string){
      |            ^
a.cc:59:12: error: expected initializer before '<' token
   59 | void mylist<T>::fromAlphabets(std::string string){
      |            ^
a.cc:69:6: error: 'string' in namespace 'std' does not name a type
   69 | std::string mylist<T>::toAlphabets(bool upper){
      |      ^~~~~~
a.cc:69:1: note: 'std::string' is defined in header '<string>'; this is probably fixable by adding '#include <string>'
   69 | std::string mylist<T>::toAlphabets(bool upper){
      | ^~~
a.cc:85:6: error: too many template-parameter-lists
   85 | void mylist<T>::add(V value){
      |      ^~~~~~
a.cc:93:12: error: expected initializer before '<' token
   93 | void mylist<T>::range(){
      |            ^
a.cc:98:12: error: expected initializer before '<' token
   98 | void mylist<T>::range(long long length){
      |            ^
a.cc:104:12: error: expected initializer before '<' token
  104 | void mylist<T>::range(long long start,long long end){
      |            ^
a.cc:154:3: error: 'mystd' has not been declared
  154 | T mystd::factorical(T a)
      |   ^~~~~
a.cc:165:3: error: 'mystd' has not been declared
  165 | T mystd::LCM(T a, T b)
      |   ^~~~~
a.cc:171:3: error: 'mystd' has not been declared
  171 | T mystd::GCD(T a, T b)
      |   ^~~~~
a.cc:191:3: error: 'mystd' has not been declared
  191 | T mystd::LCM(std::vector<T> a)
      |   ^~~~~
a.cc:212:3: error: 'mystd' has not been declared
  212 | T mystd::LCM(mylist<T> a)
      |   ^~~~~
a.cc:233:3: error: 'mystd' has not been declared
  233 | T mystd::GCD(std::vector<T> a)
      |   ^~~~~
a.cc:254:3: error: 'mystd' has not been declared
  254 | T mystd::GCD(mylist<T> a)
      |   ^~~~~
a.cc:295:33: error: 'bool mystd::Input(int, ...)' should have been declared inside 'mystd'
  295 | bool mystd::Input(int number,...){
      |                                 ^
a.cc: In function 'bool mystd::Input(int, ...)':
a.cc:297:5: error: there are no arguments to 'va_start' that depend on a template parameter, so a declaration of 'va_start' must be available [-fpermissive]
  297 |     va_start(args, number);
      |     ^~~~~~~~
a.cc:297:5: note: (if you use '-fpermissive', G++ will accept your code, but allowing the use of an undeclared name is deprecated)
a.cc:300:37: error: expected primary-expression before '*' token
  300 |         std::cin >> *(va_arg(args, T*));
      |                                     ^
a.cc:300:38: error: expected primary-expression before ')' token
  300 |         std::cin >> *(va_arg(args, T*));
      |                                      ^
a.cc:300:23: error: there are no arguments to 'va_arg' that depend on a template parameter, so a declaration of 'va_arg' must be available [-fpermissive]
  300 |         std::cin >> *(va_arg(args, T*));
      |                       ^~~~~~
a.cc: At global scope:
a.cc:306:33: error: 'void mystd::Print(int, ...)' should have been declared inside 'mystd'
  306 | void mystd::Print(int number,...){
      |                                 ^
a.cc: In function 'void mystd::Print(int, ...)':
a.cc:308:5: error: there are no arguments to 'va_start' that depend on a template parameter, so a declaration of 'va_start' must be available [-fpermissive]
  308 |     va_start(args, number);
      |     ^~~~~~~~
a.cc:314:38: error: expected primary-expression before '*' token
  314 |         std::cout << *(va_arg(args, T*));
      |                                      ^
a.cc:314:39: error: expected primary-expression before ')' token
  314 |         std::cout << *(va_arg(args, T*));
      |                                       ^
a.cc:314:24: error: there are no arguments to 'va_arg' that depend on a template parameter, so a declaration of 'va_arg' must be available [-fpermissive]
  314 |         std::cout << *(va_arg(args, T*));
      |                        ^~~~~~
a.cc: At global scope:
a.cc:320:66: error: 'void mystd::PrintArray(std::vector<T>, std::string)' should have been declared inside 'mystd'
  320 | void mystd::PrintArray(std::vector<T> array,const std::string sep){
      |                                                                  ^
a.cc:332:45: error: 'bool mystd::InputArray(std::vector<T>&)' should have been declared inside 'mystd'
  332 | bool mystd::InputArray(std::vector<T> &array){
      |                                             ^
a.cc:340:45: error: 'bool mystd::InputArrays(long long int, ...)' should have been declared inside 'mystd'
  340 | bool mystd::InputArrays(long long number,...){
      |                                             ^
a.cc: In function 'bool mystd::InputArrays(long long int, ...)':
a.cc:342:5: error: 'va_start' was not declared in this scope
  342 |     va_start(args, number);
      |     ^~~~~~~~
a.cc:342:5: note: the macro 'va_start' had not yet been defined
In file included from /usr/include/c++/14/cstdarg:43,
                 from a.cc:386:
/usr/lib/gcc/x86_64-linux-gnu/14/include/stdarg.h:50:9: note: it was later defined here
   50 | #define va_start(v,l)   __builtin_va_start(v,l)
      |         ^~~~~~~~
a.cc:346:48: error: expected primary-expression before '*' token
  346 |         i = va_arg(args, std::vector<long long>*);
      |                                                ^
a.cc:346:49: error: expected primary-expression before ')' token
  346 |         i = va_arg(args, std::vector<long long>*);
      |                                                 ^
a.cc:346:13: error: 'va_arg' was not declared in this scope
  346 |         i = va_arg(args, std::vector<long long>*);
      |             ^~~~~~
a.cc:346:13: note: the macro 'va_arg' had not yet been defined
/usr/lib/gcc/x86_64-linux-gnu/14/include/stdarg.h:53:9: note: it was later defined here
   53 | #define va_arg(v,l)     __builtin_va_arg(v,l)
      |         ^~~~~~
a.cc: At global scope:
a.cc:361:50: error: 'long long int mystd::min(std::vector<long long int>)' should have been declared inside 'mystd'
  361 | long long mystd::min(std::vector<long long> array){
      |                                                  ^
a.cc:373:50: error: 'long long int mystd::max(std::vector<long long int>)' should have been declared inside 'mystd'
  373 | long long mystd::max(std::vector<long long> array){
      |                                                  ^
a.cc: In function 'void Main()':
a.cc:456:14: error: call of overloaded 'Input<ll>(int, ll*, ll*)' is ambiguous
  456 |     Input<ll>(2,&N,&M);
      |     ~~~~~~~~~^~~~~~~~~
a.cc:295:6: note: candidate: 'bool mystd::Input(int, ...) [with T = long long int]'
  295 | bool mystd::Input(int number,...){
      |      ^~~~~
a.cc:393:17: note: candidate: 'bool mystd::Input(int, ...) [with T = long long int]'
  393 |     static bool Input(int number,...);
      |                 ^~~~~
a.cc:486:14: error: call of overloaded 'Print<ll>(int, ll*)' is ambiguous
  486 |     Print<ll>(1,&ans);
      |     ~~~~~~~~~^~~~~~~~
a.cc:306:6: note: candidate: 'void mystd::Print(int, ...) [with T = long long int]'
  306 | void mystd::Print(int number,...){
      |      ^~~~~
a.cc:401:17: note: candidate: 'void mystd::Print(int, ...) [with T = long long int]'
  401 |     static void Print(int number,...);
      |                 ^~~~~