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 <vector>
#include <cstring>
#define rep(i,n) for(int i=0;i<(n);i++)

using namespace std;

typedef long long ll;

const int INF = 1 << 30;

struct P{
	int top;
	int bottom;
};
int d[101][101];
int s[101][101];

vector <P> y;

int f (int a, int b, int c, int d )
{
	return (a*b*c*d);
}

void init_memo (int n )
{
	rep (i, n ){
		rep (j, n ){
			d[i][j] = INF;
		} // end rep
	} // end rep
}

int memoize (int i, int j )
{
	if (d[i][j] != INF )
		return d[i][j];

	int res = -1;
	if (i == j ){
		res = 0;
	}else{
		for (int k = i; k < j; k++ ){
			int q = memoize (i, k ) + memoize (k+1, j ) + f(y[i].top, y[k].bottom, y[k+1].top, y[j].bottom );
			if (q < d[i][j] ){
				res = q;
				d[i][j] = q;
				s[i][j] = k;
			} // end if
		} // end for
	} // end if

	return d[i][j] = res;
}

int main()
{
	int n;

	while (cin >> n && n ){
		init_memo (n );
		memset (s, 0, sizeof (s ) );
		y.resize(n );	
		for (int i = 0; i < n; i++ ){
			cin >> y[i].top >> y[i].bottom;
		} // end for
		memoize (0, n-1 );
		cout << d[0][n-1] << endl;
	} // end loop
		
	return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <cstdlib>
#include <memory.h>
#define rep(i, n) for(int i = 0; i < n; i++)
#define rep2(i, m, n) for(int i = m; i < n; i++)
using namespace std;


typedef long long ll;
typedef pair<int, int> P;
typedef pair<ll, P> data;
const ll INF = 1LL <<50;

data dp[200][200][2];

ll mul(data d1, data d2){
  return d1.second.first * d1.second.second * d2.second.first * d2.second.second;
}

P get_minP(data d1, data d2){
  int a = d1.second.first * d2.second.second;
  int b = d2.second.first * d1.second.second;
  if(a < b) return P(d1.second.first, d2.second.second);
  else return P(d2.second.first, d1.second.second);
}

int main(){
  int n;
  while(cin >> n && n){
    fill(&dp[0][0][0], &dp[199][199][0] + 1, data(INF, P(0, 0)));
    rep(i, n){
      cin >> dp[i][1][0].second.first >> dp[i][1][0].second.second;
      dp[i][1][1].second.first = dp[i][1][0].second.first;
      dp[i][1][1].second.second = dp[i][1][0].second.second;
      dp[i][1][0].first = dp[i][1][1].first= 0;
    }
    rep2(i, 2, n + 1){
      rep(j, n - i + 1){
	rep2(k, j + 1, j + i){
	  ll cost = dp[j][k-j][0].first + dp[k][i-(k-j)][0].first + mul(dp[j][k-j][0], dp[k][i-(k-j)][0]);
	  P m1 = get_minP(dp[j][k-j][0], dp[k][i-(k-j)][1]);
	  P m2 = get_minP(dp[j][k-j][1], dp[k][i-(k-j)][0]);
	  
	  if(cost < dp[j][i][0].first || 
	     cost == dp[j][i][0].first && m1.first * m1.second < 
	     dp[j][i][0].second.first * dp[j][i][0].second.second){
	    
	    dp[j][i][0] = dp[j][i][1] = data(cost, m1);
	  }else if(cost == dp[j][i][0].first && m1.first * m1.second == 
		   dp[j][i][0].second.first * dp[j][i][0].second.second){
	    if(m1.first < dp[j][i][0].second.first){
	      dp[j][i][0].second = m1;
	    }
	    if(m1.second < dp[j][i][1].second.second){
	      dp[j][i][1].second = m1;
	    }
	    
	     if(cost < dp[j][i][0].first || 
		cost == dp[j][i][0].first && m2.first * m2.second < 
		dp[j][i][0].second.first * dp[j][i][0].second.second){
	       
	       dp[j][i][0] = dp[j][i][1] = data(cost, m2);
	     }else if(cost == dp[j][i][0].first && m2.first * m2.second == 
		      dp[j][i][0].second.first * dp[j][i][0].second.second){
	       if(m2.first < dp[j][i][0].second.first){
		 dp[j][i][0].second = m2;
	       }
	       if(m1.second < dp[j][i][1].second.second){
		 dp[j][i][1].second = m2;
	       }
	     } 
	     
	  }
	}
      }
    }
    cout << dp[0][n][0].first << endl;
  }
  return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;

int card[110][2];
int memo[110][110];

int cost(int a, int b, int c, int d){
	return card[a][0]*card[b][1]*card[c][0]*card[d][1];
}

int f(int a, int b){
	if(memo[a][b]!=-1){
		return memo[a][b];
	}
	if(a==b){
		memo[a][b]=0;
		return memo[a][b];
	}
	
	memo[a][b]=f(a,a)+f(a+1,b)+cost(a,a,a+1,b);
	for(int i=a;i+1<=b;i++){
		memo[a][b]=min(memo[a][b],f(a,i)+f(i+1,b)+cost(a,i,i+1,b));
	}
	return memo[a][b];
}

int main(){
	
	for(int i=0;i<110;i++){
		for(int j=0;j<110;j++){
			memo[i][j]=-1;
		}
	}
	
	int n;
	cin>>n;
	
	for(int i=1;i<=n;i++){
		cin>>card[i][0]>>card[i][1];
	}
	
	cout<<f(1,n)<<endl;
	
	return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <cstring>

#define ll long long

using namespace std;

ll memo[101][101];
pair<int,int> cards[100];

ll solve(int left, int right){
    if(memo[left][right] != -1) return memo[left][right];
    
    if(right == left){
        return memo[left][right] = 0;
    }

    ll mini = (ll)1e+20;

    for(int i=left;i<right;++i){
        mini = min(mini, solve(left,i) + solve(i+1,right) + cards[left].first * cards[i].second * cards[i+1].first * cards[right].second);
    }
    
    return memo[left][right] = mini;
}

int main(){
    int n;
    while(cin >> n){
        fill((ll *)memo, (ll *)memo+101*101, -1);
        for(int i=0;i<n;++i){
            cin >> cards[i].first >> cards[i].second;
        }
        cout << solve(0, n-1) << endl;
    }
}

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

int main(){
	int n;
	int a[100],b[100];
	int cb[100][100];
	//b > a
	for(int i = 0; i < 100; i++){
		for(int j = i; j < 100; j++){
			cb[i][j] = 2147483647;
		}
	}

	cin >> n;

	for (int i = 0; i < n; i++){
		cin >> a[i] >> b[i];
	}		// 入力

	for(int i = 0; i < n; i++){
		cb[i][i] = 0;
		if(i != (n - 1)){
			cb[i][i+1] = a[i] * b[i] * a[i+1] * b[i+1];
		}
	}
	for(int i = 0; i < n; i++){
		for(int j = 0; j < n-i; j++){		//cd[j][i]
			for(int k = j; k < i+j; k++){
				cb[j][i] = min(cb[j][i] , cb[j][k]+cb[k+1][i]+a[j]*b[k]*a[k+1]*b[i]);
			}
		}
	}
	cout << cb[0][n-1] << endl;
	return 0;
}

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

struct card{
	int top, bottom;
};

int main(){
	int n;
	unsigned int cost = 0;
	vector<card> cards;
	cin >> n;
	for(int i = 0; i < n; i++){
		card c;
		cin >> c.top >> c.bottom; 
		cards.push_back(c);
	}
	while(cards.size() != 1){
		int t, b;
		int nmax = -1;
		for(int i = 0; i < cards.size(); i++){
			for(int j = 0; i < cards.size(); i++){
				if(i != j){
					if(cards[i].bottom * cards[j].top > nmax){
						nmax = cards[i].bottom * cards[j].top;
						t = i;
						b = j;
					}
				}
			}
		}
		cost += cards[t].top * cards[t].bottom * cards[b].top * cards[b].bottom;
		cards[t].bottom = cards[b].bottom;
		cards[b] = cards[cards.size() - 1];
		cards.pop_back();
	}
	cout << cost << endl;
	return 0;
}