Go: package main

import (
	"bufio"
	"os"
	"strconv"
	"strings"
	"sort"
	"fmt"
)

var scanner = bufio.NewScanner(os.Stdin)
const INF = 0x7fffffff
func main() {
	scanner.Scan()
	n, _ := strconv.Atoi(scanner.Text())
	top := make([]int, 0)
	bottom := make([]int, 0)
	for i := 0; i < n; i++ {
		scanner.Scan()
		buf := strings.Split(scanner.Text(), " ")
		a, _ := strconv.Atoi(buf[0])
		b, _ := strconv.Atoi(buf[1])
		top = append(top, a)
		bottom = append(bottom, b)
	}
	dp := make([][]int, n+1)
	for i := range dp {
		dp[i] = make([]int, n+1)
	}
	for i := 0; i < n; i++ {
		for j := 0; j < n; j++ {
			dp[i][j] = INF
		}
	}
	for j := 0; j < n; j++ {
		dp[0][j] = 0
	}
	for i := 1; i < n; i++ {
		for j := 0; j < n-i; j++ {
			tmp := make([]int, 0)
			for k := 0; k < i; k++ {
				tmp = append(tmp, dp[k][j] + dp[i - k - 1][j + k + 1] +top[j] * bottom[j + k] * top[j + k + 1] * bottom[j + i])
			}
			sort.Ints(tmp)
			dp[i][j] = tmp[0]
		}
	}
	fmt.Println(dp[n-1][0])
}

func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}

C++: #include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <deque>
#include <complex>
#include <stack>
#include <queue>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <ctime>
#include <iterator>
#include <bitset>
#include <numeric>
#include <list>
#include <iomanip>

#if __cplusplus >= 201103L
#include <array>
#include <tuple>
#include <initializer_list>
#include <unordered_set>
#include <unordered_map>
#include <forward_list>

#define cauto const auto&
#else

#endif

using namespace std;


namespace{
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

typedef vector<int> vint;
typedef vector<vector<int> > vvint;
typedef vector<long long> vll, vLL;
typedef vector<vector<long long> > vvll, vvLL;

#define VV(T) vector<vector< T > >

template <class T>
void initvv(vector<vector<T> > &v, int a, int b, const T &t = T()){
    v.assign(a, vector<T>(b, t));
}

template <class F, class T>
void convert(const F &f, T &t){
    stringstream ss;
    ss << f;
    ss >> t;
}


#define reep(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,n) reep((i),0,(n))
#define ALL(v) (v).begin(),(v).end()
#define PB push_back
#define F first
#define S second
#define mkp make_pair
#define RALL(v) (v).rbegin(),(v).rend()


#define MOD 1000000007LL
#define EPS 1e-8
static const int INF=1<<24;

void mainmain(){
	int n;
	cin>>n;
	vector<pll> v(n);
	rep(i,3){
		cin>>v[i].F>>v[i].S;
	}
	ll ans=0;
	while(v.size()!=1){
		// rep(i,v.size()){
			// cout<<v[i].F<<" "<<v[i].S<<endl;
		// }
		// cout<<endl;
		ll x=-1,y=0;
		rep(i,v.size()-1){
			if(y<v[i].F*v[i+1].S){
				x=i;
				y=v[i].F*v[i+1].S;
			}
			if(y<v[i].S*v[i+1].F){
				x=i;
				y=v[i].S*v[i+1].F;
			}
		}
		ans+=v[x].F*v[x].S*v[x+1].F*v[x+1].S;
		if(v[x].F*v[x+1].S>v[x].S*v[x+1].F) v[x]=mkp(v[x+1].F,v[x].S);
		else v[x]=mkp(v[x].F,v[x+1].S);
		reep(i,x+1,v.size()-1){
			swap(v[i],v[i+1]);
		}
		v.pop_back();
	}
	cout<<ans<<endl;
}



}
main() try{
    mainmain();
}
catch(...){
}

C++: #include<stdio.h>
#include<stdlib.h>
#include<iostream>
using namespace std;

#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define in(x) cin >> x
#define out(str) cout << str << endl

template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }

int N,mini=1000,abpos,ipos,miniab=0,ans=0,a1,a2,b1,b2;

int main(){
    cin>>N;
    int a[N],b[N];
    rep(i,N){
        cin>>a[i]>>b[i];
        if(mini>a[i]){
            if(miniab<b[i]){
                mini=a[i];
                abpos=0;
                ipos=i;
                miniab=b[i];
            }
        }
        if(mini>b[i]){
            if(miniab<a[i]){
                mini=b[i];
                abpos=1;
                ipos=i;
                miniab=a[i];
            }
        }
    }
    if(abpos==0){
        a1=mini;
        b1=b[ipos];
        rep(i,N){
            if(i==ipos)continue;
            a2=a[i];
            b2=b[i];
            ans+=a1*b1*a2*b2;
            b1=b2;
        }
    }
    else{
        a2=a[ipos];
        b2=mini;
        rep(i,N){
            if(i==ipos)continue;
            a1=a[i];
            b1=b[i];
            ans+=a1*b1*a2*b2;
            a2=a1;
        }
    }
    out(ans);
}

C++: #include <iostream>
#include <vector>
using namespace std;

using ll = long long;

constexpr ll INF = 1e18;

int main() {
    int n;
    cin >> n;
    vector<vector<ll>> dp(n, vector<ll>(n, INF));
    vector<ll> a(n), b(n);
    for(int i=0; i<n; ++i) {
        cin >> a[i] >> b[i];
    }
    for(int i=0; i<n; ++i) {
        dp[i][i] = 0;
    }
    for(int w=1; w<n; ++w) {
        for(int i=0; i+w<n; ++i) {
            for(int j=i; j<i+w; ++j) {
                dp[i][i+w] = min(dp[i][i+w], dp[i][j] + dp[j+1][i+w] + a[i]*b[j]*a[j+1]*b[i+w]);
            }
        }
    }
    cout << dp[0][n-1] << endl;
}

C++: #include <iostream>
#include <algorithm>
using namespace std;
int n, a[100], b[100]; long long dp[100][101];
int main() {
	cin >> n;
	for (int i = 0; i < n; i++) cin >> a[i] >> b[i];
	for (int i = 0; i < n; i++) {
		for (int j = 0; j <= n; j++) dp[i][j] = j - i - 1 ? (1LL << 60) : 0;
	}
	for (int i = 2; i <= n; i++) {
		for (int j = 0; i + j <= n; j++) {
			for (int k = j; k < i + j; k++) dp[j][i + j] = min(dp[j][i + j], dp[j][k] + dp[k][i + j] + a[j] * b[k - 1] * a[k] * b[i + j - 1]);
		}
	}
	cout << dp[0][n] << endl;
	return 0;
}

C++: #include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<int,int> pii;
typedef vector<int> VI;
typedef vector<VI> VVI;
const double PI = 3.14159265358979323846;
const double EPS = 1e-12;
const int INF = numeric_limits<int>::max() / 2;
const int NEG_INF = numeric_limits<int>::min() / 2;

int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);

  int n;cin>>n;
  std::vector<int> a(n),b(n);
  for(int i=0;i<n;i++) cin>>a[i]>>b[i];
  ll dp[n+1][n+1];
  for(int i=0;i<n;i++)for(int j=0;j<n;j++) dp[i][j]=INF;
  for(int i=0;i<n;i++) dp[i][i]=0;
  for(int k=0;k<n-1;k++){
      for(int i=0;i<n;i++){
          for(int j=i+1;j<n;j++){
              ll cost=a[i]*b[k]*a[k+1]*b[j];
              dp[i][j]=min(dp[i][j],dp[i][k]+dp[k+1][j]+cost);
          }
      }
  }
  cout<<dp[0][n-1]<<endl;
}

C++: #include <iostream>
#define INF 1e+9
using namespace std;

int main(){
	int n,a[100],b[100];
	int dp[100][100];
	cin >> n;
	for(int i = 0;i < n;i++){
		for(int j = 0;j < n;j++) dp[i][j] = INF;
	}
	for(int i = 0;i < n;i++) {
		cin >> a[i] >> b[i];
		dp[i][i] = 0;
	}
	for(int i = 1;i < n;i++){
		for(int j = 0;j < n - i;j++){
			for(int k = 0;k < i;k++){
				dp[j][j + i] = min(dp[j][j + i],dp[j][j + k] + dp[j + k + 1][j + i] + a[j] * b[j + k] * a[j + k + 1] * b[j + i]);
			}
		}
	}
	cout << dp[0][n - 1] << endl;
	return 0;
}

C++: #include <iostream>
#include <vector>
#include <climit>
#include <map>

using namespace std;

typedef pair<int, int> P;
vector<P> D;

int n, a, b;
int DP[101][101];

void solve()
{
	for(int i=0; i<n; i++)
	{
		for(int j=0; j<n; j++)
		{
			if(i!=j) DP[i][j] = INT_MAX;
			else DP[i][j] = 0;
		}
	}	

	for(int i=1; i<n; i++)
	{
		for(int j=0; j<n-i; j++)
		{
			for(int k=j; k<j+i; k++)
			{
				int cost = D[j].first * D[k].second * D[k+1].first * D[j+i].second;
				DP[j][j+i] = min(DP[j][j+i], cost + DP[j][k] + DP[k+1][j+i]);
			}
		}
	}

	cout << DP[0][n-1] << endl;
}

int main(int argc, char** argv)
{
	cin >> n;
	for(int i=0; i<n; i++)
	{
		cin >> a >> b;
		P p(a, b);
		D.push_back(p);
	}

	solve();
}