Language: C++

#include "bits/stdc++.h"
using namespace std;
#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))
#define rer(i,l,u) for(int (i)=(int)(l);(i)<=(int)(u);++(i))
#define reu(i,l,u) for(int (i)=(int)(l);(i)<(int)(u);++(i))
static const int INF = 0x3f3f3f3f; static const long long INFL = 0x3f3f3f3f3f3f3f3fLL;
typedef vector<int> vi; typedef pair<int, int> pii; typedef vector<pair<int, int> > vpii; typedef long long ll;
template<typename T, typename U> static void amin(T &x, U y) { if(y < x) x = y; }
template<typename T, typename U> static void amax(T &x, U y) { if(x < y) x = y; }

template<int... Sizes>
struct MultidimensionalIndex;
template<>
struct MultidimensionalIndex<> {
	MultidimensionalIndex() {}
	int getTotalSize() const { return 1; }
	int getIndexAcc(int acc) const { return acc; }
};
template<int Head, int... Tail>
struct MultidimensionalIndex<Head, Tail...> : MultidimensionalIndex<Tail...> {
	using Base = MultidimensionalIndex<Tail...>;
	enum { size = Head };
	template<typename... TailT>
	MultidimensionalIndex(TailT... tail) : Base(tail...) {}
	int getTotalSize() const { return size * Base::getTotalSize(); }
	template<typename... TailT>
	int getIndexAcc(int acc, int head, TailT... tail) const { return Base::getIndexAcc(acc * size + head, tail...); }
};
template<int... Tail>
struct MultidimensionalIndex<0, Tail...> : MultidimensionalIndex<Tail...> {
	using Base = MultidimensionalIndex<Tail...>;
	const int size;
	template<typename... TailT>
	MultidimensionalIndex(int head, TailT... tail) : size(head), MultidimensionalIndex<Tail...>(tail...) {}
	int getTotalSize() const { return size * Base::getTotalSize(); }
	template<typename... TailT>
	int getIndexAcc(int acc, int head, TailT... tail) const { return Base::getIndexAcc(acc * size + head, tail...); }
};

template<typename Val, int... Sizes>
struct DP : MultidimensionalIndex<Sizes...> {
	std::vector<Val> dp;
	template<typename... SizesT>
	DP(Val init, SizesT... sizes) : MultidimensionalIndex<Sizes...>(sizes...), dp(MultidimensionalIndex<Sizes...>::getTotalSize(), init) {}
	template<typename... SizesT>
	Val &operator()(SizesT... indices) { return dp[getIndexAcc(0, indices...)]; }
	template<typename... SizesT>
	Val operator()(SizesT... indices) const { return dp[getIndexAcc(0, indices...)]; }
	void swap(DP &that) { dp.swap(that); }
};

struct Interval {
	int L, R, X;
	bool operator<(const Interval &that) const {
		return X < that.X;
	}
};

int main() {
	int N;
	while(~scanf("%d", &N)) {
		vector<Interval> intervals(N);
		rep(i, N) {
			int L; int R;
			scanf("%d%d", &L, &R);
			intervals[i] = { L, R, L + R };
		}
		sort(intervals.begin(), intervals.end());
		reverse(intervals.begin(), intervals.end());
		DP<ll, 0, 0, 2> dp(INFL, N + 1, N + 1);
		dp(0, 0, 0) = 0;
		rep(i, N) {
			const Interval a = intervals[i];
			rer(l, 0, i) rep(c, l == i ? 1 : 2) {
				int r = i - l - c;
				ll x = dp(l, r, c);
				if(x == INFL) continue;
				amin(dp(l + 1, r, c), x + a.R + (ll)a.X * l);
				amin(dp(l, r + 1, c), x + a.L + (ll)a.X * r);
				if(c == 0)
					amin(dp(l, r, c + 1), x + (ll)a.X * (N / 2));
			}
		}
		ll ans = INFL;
		rer(l, 0, N - N % 2) {
			int r = N - N % 2 - l;
			amin(ans, dp(l, r, N % 2));
		}
		printf("%lld\n", ans);
	}
	return 0;
}


Compiler output:
a.cc: In instantiation of 'Val& DP<Val, Sizes>::operator()(SizesT ...) [with SizesT = {int, int, int}; Val = long long int; int ...Sizes = {0, 0, 2}]':
a.cc:71:5:   required from here
   71 |                 dp(0, 0, 0) = 0;
      |                 ~~^~~~~~~~~
a.cc:46:67: error: 'getIndexAcc' was not declared in this scope, and no declarations were found by argument-dependent lookup at the point of instantiation [-fpermissive]
   46 |         Val &operator()(SizesT... indices) { return dp[getIndexAcc(0, indices...)]; }
      |                                                        ~~~~~~~~~~~^~~~~~~~~~~~~~~
a.cc:46:67: note: declarations in dependent base 'MultidimensionalIndex<0, 0, 2>' are not found by unqualified lookup
a.cc:46:67: note: use 'this->getIndexAcc' instead