Language: C++

#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;

int main(){
	int n, x;
	cin >> n >> x;
	vector<int> a(n);
	int ans = 0;
	for(int i = 0; i < n; i++){
		cin >> a.at(i);
		if(a.at(i) > x){
			ans += a.at(i) - x;
			a.at(i) = x;
		}
	}
	vector<int> b = a;
	long long ans2 = ans;
	// for(int i = 0; i < n; i++){
	// 	cout << a.at(i) << " ";
	// }
	// cout << endl;
	for(int i = 1; i < n; i++){
		if(a.at(i - 1) + a.at(i) < x){
			continue;
		}
		else if(a.at(i) == x){
			ans += a.at(i - 1);
			a.at(i) -= a.at(i - 1);
		}
		else{
			if(a.at(i - 1) == x){
				ans += a.at(i);
				a.at(i) = 0;
			}
			else{
				ans += a.at(i - 1) + a.at(i) - x;
				a.at(i) = x - a.at(i - 1);
			}
		}
	}
	long long ans2 = 0;
	for(int i = n - 2; i >= 0; i--){
		if(b.at(i + 1) + b.at(i) < x){
			continue;
		}
		else if(b.at(i) == x){
			ans2 += b.at(i + 1);
			b.at(i) -= b.at(i + 1);
		}
		else{
			if(b.at(i + 1) == x){
				ans2 += b.at(i);
				b.at(i) = 0;
			}
			else{
				ans2 += b.at(i + 1) + b.at(i) - x;
				b.at(i) = x - b.at(i + 1); 
			}
		}
	}
	cout << min(ans, ans2) << endl;
	return 0;
}

Compiler output:
a.cc: In function 'int main()':
a.cc:42:19: error: redeclaration of 'long long int ans2'
   42 |         long long ans2 = 0;
      |                   ^~~~
a.cc:18:19: note: 'long long int ans2' previously declared here
   18 |         long long ans2 = ans;
      |                   ^~~~
a.cc:62:20: error: no matching function for call to 'min(int&, long long int&)'
   62 |         cout << min(ans, ans2) << endl;
      |                 ~~~^~~~~~~~~~~
In file included from /usr/include/c++/14/algorithm:60,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:51,
                 from a.cc:2:
/usr/include/c++/14/bits/stl_algobase.h:233:5: note: candidate: 'template<class _Tp> constexpr const _Tp& std::min(const _Tp&, const _Tp&)'
  233 |     min(const _Tp& __a, const _Tp& __b)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:233:5: note:   template argument deduction/substitution failed:
a.cc:62:20: note:   deduced conflicting types for parameter 'const _Tp' ('int' and 'long long int')
   62 |         cout << min(ans, ans2) << endl;
      |                 ~~~^~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:281:5: note: candidate: 'template<class _Tp, class _Compare> constexpr const _Tp& std::min(const _Tp&, const _Tp&, _Compare)'
  281 |     min(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:281:5: note:   candidate expects 3 arguments, 2 provided
In file included from /usr/include/c++/14/algorithm:61:
/usr/include/c++/14/bits/stl_algo.h:5686:5: note: candidate: 'template<class _Tp> constexpr _Tp std::min(initializer_list<_Tp>)'
 5686 |     min(initializer_list<_Tp> __l)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5686:5: note:   candidate expects 1 argument, 2 provided
/usr/include/c++/14/bits/stl_algo.h:5696:5: note: candidate: 'template<class _Tp, class _Compare> constexpr _Tp std::min(initializer_list<_Tp>, _Compare)'
 5696 |     min(initializer_list<_Tp> __l, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5696:5: note:   template argument deduction/substitution failed:
a.cc:62:20: note:   mismatched types 'std::initializer_list<_Tp>' and 'int'
   62 |         cout << min(ans, ans2) << endl;
      |                 ~~~^~~~~~~~~~~