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 <iostream>
#include <string>
#include <vector>
#include <cstring>
#include <climits>
using namespace std;

int n;
int top[100], bottom[100];
int dp[100][100];

int solve(int l, int r) {
    if (0 <= dp[l][r]) return dp[l][r];
    if (l == r) return 0;
    int ret = INT_MAX;
    for (int i=l+1; i<=r; ++i) {
        ret = min(ret, solve(l,i-1) + solve(i,r) + top[l] * bottom[i-1] * top[i] * bottom[r]);
    }
    return dp[l][r] = ret;
}

int main() {
    memset(dp, -1, sizeof dp);
    cin >> n;
    for (int i=0; i<n; ++i) {
        cin >> top[i] >> bottom[i];
    }
    cout << solve(0, n-1) << endl;
    return 0;
}

C++: #include <iostream>
#include <stdio.h>
#include <algorithm>

using namespace std;

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

C++: #include<iostream>
#include<cstdio>
#define REP(i,s,n) for(int i=s;i<n;i++)
#define rep(i,n) REP(i,0,n)
#define inf (1<<28)
using namespace std;
typedef pair<int,int> ii;
ii p[200];
int n;

void solve()
{
  int M[n+1][n+1];
  rep(i,n+1)rep(j,n+1)M[i][j] = inf;
  rep(i,n+1)M[i][i] = 0;

  rep(j,n)
    {
      rep(i,n-j)
	{
	  REP(k,i+1,i+j+1)
	    {
	      int cost = M[i][k-1] + M[k][i+j] + p[i].first*p[k-1].second*p[k].first*p[i+j].second;
	      if(cost < M[i][i+j])
		{
		  M[i][i+j] = cost;
		  if(i == 0 && i+j == n-1)
		    {
		      //cout << cost << " = M["<<i<<"]["<<k-1<<"] + M["<<k<<"]["<<i+j<<"] + p["<<i<<"].first*p["<<k-1<<"].second*p["<<k<<"].first*p["<<i+j<<"].second" << endl; 
		      //cout <<  M[i][k-1] <<"+"<< M[k][i+j] <<"+"<< p[i].first<<"*"<<p[k-1].second<<"*"<<p[k].first<<"*"<<p[i+j].second;
		      //cout << endl;
		    }
		}
	    }
	}
    }
  cout << M[0][n-1] << endl;
}

int main()
{

  while(cin >> n)
    {
      rep(i,n)cin >> p[i].first >> p[i].second;
      solve();
    }
  return 0;
}

C++: #include <cstdio>
#include <algorithm>
#include <cstring>
#define INF (1LL<<31)-1
using namespace std;
int n;
int a[101],b[101];
int dp[101][101];

int main(void){
	scanf("%d",&n);
	for(int i=0;i<n;i++){
		scanf("%d%d",&a[i],&b[i]);
	}
	for(int i=0;i<n;i++){
		for(int j=0;j<=n;j++){
			dp[i][j]=INF;
		}
		dp[i][i+1]=0;
	}
	for(int i=1;i<n;i++){
		for(int j=0;j<n-i;j++){
			if(dp[j][j+i]<INF){
				dp[j][j+i+1]=min(dp[j][j+i+1],dp[j][j+i]+a[j]*a[j+i]*b[j+i-1]*b[j+i]);
			}
		}
		for(int j=n-i;j>=1;j--){
			if(dp[j][j+i]<INF){
				dp[j-1][j+i]=min(dp[j-1][j+i],dp[j][j+i]+a[j-1]*a[j]*b[j-1]*b[j+i-1]);
			}
		}
	}
	printf("%d\n",dp[0][n]);
	return 0;
}

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

int main(){
  int n,a[100],b[100];
  int dp[100][100];

  cin >> n;
  for(int i=0;i<n;i++)cin >> a[i] >> b[i];

  for(int i=0;i<n;i++){
    fill(dp[i],dp[i]+n,(1<<31)-1);
    dp[i][i] = 0;
  }
  for(int i=1;i<n;i++){
    for(int j=0;j+i<n;j++){
      for(int k=j;k<j+i;k++){
	dp[j][j+i] = min(dp[j][j+i],dp[j][k]+dp[k+1][j+i]+a[j]*b[k]*a[k+1]*b[j+i]);
      }
    }
  }

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

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,n){
		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<bits/stdc++.h>
#define range(i,a,b) for(int i = (a); i < (b); i++)
#define rep(i,b) for(int i = 0; i < (b); i++)
#define all(a) (a).begin(), (a).end()
#define show(x)  cerr << #x << " = " << (x) << endl;
const long long INF = (1LL << 60);
using namespace std;

long long dp[105][205][205];

int main(){
    int n;
    cin >> n;

    pair<int, int> p[105];
    rep(i,n) cin >> p[i].first >> p[i].second;

    rep(i,105) rep(j,205) rep(k,205) dp[i][j][k] = INF;

    rep(l,n){
        for(int i = n; i > 1; i--){
            range(j,1,201){
                range(k,1,201){
                    long long next = dp[i - 1][j][k] + j * k * p[l].first * p[l].second;
                    dp[i][j][p[l].second] = min(dp[i][j][p[l].second], next);
                    dp[i][p[l].first][k] = min(dp[i][p[l].first][k], next);
                }
            }
        }
        dp[1][p[l].first][p[l].second] = 0;
    }

    long long mini = INF;
    rep(i,201) rep(j,201)  mini = min(mini, dp[n][i][j]);
    cout << mini << endl;
}

C++: #include <iostream>
using namespace std;
int n, a[100], b[100], 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++) dp[i][i + 1] = 0;
	for (int i = 2; i <= n; i++) {
		for (int j = 0; i + j <= n; j++) {
			for (int k = i; k < i + j; k++) dp[i][j] = min(dp[i][j], dp[i][k] + dp[k][j] + a[i] * b[k - 1] * a[k] * b[j - 1]);
		}
	}
	cout << dp[0][n] << endl;
	return 0;
}