Language: C++

#include<bits/stdc++.h>

using namespace std;

int main() {
	long long int K, T;
	cin >> K >> T;
	vector<long long int>a( T );
	priority_queue<pair<long long int, long long int>, vector<long long int, long long int>, greater<pair<long long int, long long int>>>que;
	for( size_t i = 0; i < T; i++ ) {
		cin >> a[i];
		que.push( make_pair( a[i], i ) );
	}
	long long int prev = -1;
	int f = 0;
	long long int ans = 0;
	while( que.size() ) {
		pair<long long int, long long int> now = que.top(); que.pop();
		if( now.second == prev ) {
			if( que.size() ) {
				pair<long long int, long long int> next = que.top(); que.pop();
				next.first--;
				prev = next.second;
				if( next.first != 0 ) {
					que.push( next );
				}
				if( now.first != 0 ) {
					que.push( now );
				}
			} else {
				ans = now.first;
				break;
			}
		} else {
			now.first--;
			if( now.first != 0 ) {
				que.push( now );
			}
		}
	}
	cout << ans << endl;
}


Compiler output:
In file included from /usr/include/c++/14/vector:66,
                 from /usr/include/c++/14/functional:64,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:53,
                 from a.cc:1:
/usr/include/c++/14/bits/stl_vector.h: In instantiation of 'struct std::_Vector_base<long long int, long long int>':
/usr/include/c++/14/bits/stl_vector.h:428:11:   required from 'class std::vector<long long int, long long int>'
  428 |     class vector : protected _Vector_base<_Tp, _Alloc>
      |           ^~~~~~
/usr/include/c++/14/bits/stl_queue.h:520:67:   required from 'class std::priority_queue<std::pair<long long int, long long int>, std::vector<long long int, long long int>, std::greater<std::pair<long long int, long long int> > >'
  520 |       static_assert(is_same<_Tp, typename _Sequence::value_type>::value,
      |                                                                   ^~~~~
a.cc:9:135:   required from here
    9 |         priority_queue<pair<long long int, long long int>, vector<long long int, long long int>, greater<pair<long long int, long long int>>>que;
      |                                                                                                                                              ^~~
/usr/include/c++/14/bits/stl_vector.h:87:28: error: 'long long int' is not a class, struct, or union type
   87 |         rebind<_Tp>::other _Tp_alloc_type;
      |                            ^~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_vector.h:89:9: error: 'long long int' is not a class, struct, or union type
   89 |         pointer;
      |         ^~~~~~~
/usr/include/c++/14/bits/stl_vector.h: In instantiation of 'class std::vector<long long int, long long int>':
/usr/include/c++/14/bits/stl_queue.h:520:67:   required from 'class std::priority_queue<std::pair<long long int, long long int>, std::vector<long long int, long long int>, std::greater<std::pair<long long int, long long int> > >'
  520 |       static_assert(is_same<_Tp, typename _Sequence::value_type>::value,
      |                                                                   ^~~~~
a.cc:9:135:   required from here
    9 |         priority_queue<pair<long long int, long long int>, vector<long long int, long long int>, greater<pair<long long int, long long int>>>que;
      |                                                                                                                                              ^~~
/usr/include/c++/14/bits/stl_vector.h:518:20: error: '_M_allocate' has not been declared in 'std::vector<long long int, long long int>::_Base'
  518 |       using _Base::_M_allocate;
      |                    ^~~~~~~~~~~
/usr/include/c++/14/bits/stl_vector.h:519:20: error: '_M_deallocate' has not been declared in 'std::vector<long long int, long long int>::_Base'
  519 |       using _Base::_M_deallocate;
      |                    ^~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_vector.h:521:20: error: '_M_get_Tp_allocator' has not been declared in 'std::vector<long long int, long long int>::_Base'
  521 |       using _Base::_M_get_Tp_allocator;
      |                    ^~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/14/queue:66,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:157:
/usr/include/c++/14/bits/stl_queue.h: In instantiation of 'class std::priority_queue<std::pair<long long int, long long int>, std::vector<long long int, long long int>, std::greater<std::pair<long long int, long long int> > >':
a.cc:9:135:   required from here
    9 |         priority_queue<pair<long long int, long long int>, vector<long long int, long long int>, greater<pair<long long int, long long int>>>que;
      |                                                                                                                                              ^~~
/usr/include/c++/14/bits/stl_queue.h:520:67: error: static assertion failed: value_type must be the same as the underlying container
  520 |       static_assert(is_same<_Tp, typename _Sequence::value_type>::value,
      |                                                                   ^~~~~
/usr/include/c++/14/bits/stl_queue.h:520:67: note: 'std::integral_constant<bool, false>::value' evaluates to false
In file included from /usr/include/c++/14/cassert:43,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:33:
/usr/include/c++/14/bits/stl_vector.h: In instantiation of 'std::_Vector_base<_Tp, _Alloc>::_Vector_impl::_Vector_impl() [with _Tp = long long int; _Alloc = long long int]':
/usr/include/c++/14/type_traits:1110:25:   required by substitution of 'template<class _Tp, class ... _Args> using std::__is_constructible_impl = std::__bool_constant<__is_constructible(_Tp, _Args ...)> [with _Tp = std::vector<long long int, long long int>; _Args = {}]'
 1110 |       = __bool_constant<__is_constructible(_Tp, _Args...)>;
      |                         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/type_traits:1124:12:   required from 'struct std::is_default_constructible<std::vector<long long int, long long int> >'
 1124 |     struct is_default_constructible
      |            ^~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/type_traits:183:35:   required by substitution of 'template<class ... _Bn> std::__detail::__first_t<std::integral_constant<bool, true>, typename std::enable_if<(bool)(_Bn::value), void>::type ...> std::__detail::__and_fn(int) [with _Bn = {std::is_default_constructible<std::greater<std::pair<long long int, long long int> > >, std::is_default_constructible<std::vector<long long int, long long int> >}]'
  183 |                                       __enable_if_t<bool(_Bn::value)>...>;
      |                                                               ^~~~~
/usr/include/c++/14/type_traits:199:42:   required from 'struct std::__and_<std::is_default_constructible<std::greater<std::pair<long long int, long long int> > >, std::is_default_constructible<std::vector<long long int, long long int> > >'
  199 |     : decltype(__detail::__and_fn<_Bn...>(0))
      |                ~~~~~~~~~~~~~~~~~~~~~~~~~~^~~
/usr/include/c++/14/bits/stl_queue.h:551:43:   required by substitution of 'template<class _Seq, class _Requires> std::priority_queue<std::pair<long long int, long long int>, std::vector<long long int, long long int>, std::greater<std::pair<long long int, long long int> > >::priority_queue() [with _Seq = std::vector<long long int, long long int>; _Requires = <missing>]'
  551 |       template<typename _Seq = _Sequence, typename _Requires = typename
      |                                           ^~~~~~~~
a.cc:9:135:   required from here
    9 |         priority_queue<pair<long long int, long long int>, vector<long long int, long long int>, greater<pair<long long int, long long int>>>que;
      |                                                                                                                                              ^~~
/usr/include/c++/14/bits/stl_vector.h:136:24: error: 'long long int' is not a class, struct, or union type
  136 |         _Vector_impl() _GLIBCXX_NOEXCEPT_IF(
      |                        ^~~~~~~~~~~~~~~~~~~~
a.cc: In function 'int main()':
a.cc:12:25: error: no matching function for call to 'std::priority_queue<std::pair<long long int, long long int>, std::vector<long long int, long long int>, std::greater<std::pair<long long int, long long int> > >::push(std::pair<long long int, long unsigned int>)'
   12 |                 que.push( make_pair( a[i], i ) );
      |                 ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_queue.h:736:7: note: candidate: 'void std::priority_queue<_Tp, _Sequence, _Compare>::push(const value_type&) [with _Tp = std::pair<long long int, long long int>; _Sequence = std::vector<long long int, long long int>; _Compare = std::greater<std::pair<long long int, long long int> >; value_type = long long int]'
  736 |       push(const value_type& __x)
      |       ^~~~
/usr/include/c++/14/bits/stl_queue.h:736:30: note:   no known conversion for argument 1 from 'std::pair<long long int, long unsigned int>' to 'const std::priority_queue<std::pair<long long int, long long int>, std::vector<long long int, long long int>, std::greater<std::pair<long long int, long long int> > >::value_type&' {aka 'const long long int&'}
  736 |       push(const value_type& __x)
      |            ~~~~~~~~~~~~~~~~~~^~~
/usr/include/c++/14/bits/stl_queue.h:744:7: note: candidate: 'void std::priority_queue<_Tp, _Sequence, _Compare>::push(value_type&&) [with _Tp = std::pair<long long int, long long int>; _Sequence = std::vector<long long int, long long int>; _Compare = std::greater<std::pair<long long int, long long int> >; value_type = long long int]'
  744 |       push(value_type&& __x)
      |       ^~~~
/usr/include/c++/14/bits/stl_queue.h:744:25: note:   no known conversion for argument 1 from 'std::pair<long long int, long unsigned int>' to 'std::priority_queue<std::pair<long long int, long long int>, std::vector<long long int, long long int>, std::greater<std::pair<long long int, long long int> > >::value_type&&' {aka 'long long int&&'}
  744 |       push(value_type&& __x)
      |            ~~~~~~~~~~~~~^~~
a.cc:18:62: error: 'class std::priority_queue<std::pair<long long int, long long int>, std::vector<long long int, long long int>, std::greater<std::pair<long long int, long long int> > >' has no member named 'top'; did you mean 'pop'?
   18 |                 pair<long long int, long long int> now = que.top(); que.pop();
      |                                                              ^~~
      |                                                              pop
a.cc:21:79: error: 'class std::priority_queue<std::pair<long long int, long long int>, std::vector<long long int, long long int>, std::greater<std::pair<long long int, long long int> > >' has no member named 'top'; did you mean 'pop'?
   21 |                                 pair<long long int, long long int> next = que.top(); que.pop();
      |                                                                               ^~~
      |