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 <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(m2.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>
#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 = 1e9 + 5;
using namespace std;

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

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

    vector<vector<vector<long long>>> dp(105,vector<vector<long long>>(205,vector<long long>(205,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>
#include<algorithm>
#include<climits>
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<ctime>
#include<string>
#include<cstring>
#include<vector>
#include<stack>
#include<queue>
#include<set>
#include<map>
#include<bitset>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> i_i;
typedef pair<long long, int> ll_i;

#define PI 3.141592653589793238462643383279
#define mod 1000000007LL
#define rep(i, n) for(i = 0;i < n;++i)
#define rep1(i, n) for(i = 1;i < n;++i)
#define per(i, n) for(i = n - 1;i > -1;--i)
#define int(x) int x; scanf("%d",&x)
#define int2(x, y) int x, y; scanf("%d%d",&x, &y)
#define int3(x, y, z) int x, y, z; scanf("%d%d%d",&x, &y, &z)
#define int4(v, x, y, z) int v, x, y, z; scanf("%d%d%d%d", &v, &x, &y, &z)
#define int5(v, w, x, y, z) int v, w, x, y, z; scanf("%d%d%d%d%d", &v, &w, &x, &y, &z)
#define pri(x) cout << (x) << "\n"
#define pri2(x, y) cout << (x) << " " << (y) << "\n"
#define pri3(x, y, z) cout << (x) << " " << (y) << " " << (z) << "\n"
#define pb push_back
#define mp make_pair
#define all(a) (a).begin(),(a).end()
#define kabe puts("---------------------------")
#define kara puts("")
#define debug(x) cout << " --- " << (x) << "\n"
#define debug2(x, y) cout << " --- " << (x) << " " << (y) << "\n"
#define debug3(x, y, z) cout << " --- " << (x) << " " << (y) << " " << (z) << "\n"
#define X first
#define Y second
#define eps 0.0001
#define prid(x) printf("%.15lf\n", x)

int a[100], b[100];
ll dp[100][101] = {0};

signed main(void){
  int i, j;
  for(int testcase = 0;testcase >= 0;testcase++){
    int(n);
    rep(i, n)rep(j, n + 1)dp[i][j] = (1LL << 32);
    rep(i, n)scanf("%d%d", a+i, b+i);
    rep(i, n)dp[i][i + 1] = 0;
    rep(i, n - 1)dp[i][i + 2] = a[i] * b[i] * a[i + 1] * b[i + 1];

    for(j = 3;j <= n;++j)rep(i, n - j + 1)for(int k = 1;k < j - 1;++k)
    dp[i][i + j] = min(dp[i][i + j], dp[i][i + k] + dp[i + k][i + j] + a[i] * b[i + k - 1] * a[i + k] * b[i + j - 1]);

    pri(dp[0][n]);

/*/

//*/ break;
  }
  return 0;
}

C++: #include <iostream>
#include <vector>
#include <climits>
#include <iomanip>

using namespace std;

long long dp[101][101];
vector<pair<int, int> > cards;
int n;

/*void print_table(){
  for(int i=0;i<=n;i++){
    for(int j=0;j<=n;j++){
      if(dp[i][j] == INT_MAX){
	cout << "INF";
      }else{
	cout << setw(3) << dp[i][j];
      }
      cout << ' ';
    }
    cout << endl;
  }
  cout << endl;
  }*/

main(){
  cin>>n;
  for(int i=0;i<n;i++){
    int a, b;
    cin >> a >> b;
    cards.push_back(make_pair(a, b));
  }
  for(int i=0;i<=n;i++){
    for(int j=0;j<=n;j++){
      dp[i][j] = INT_MAX;
    }
    dp[i][i] = 0;
  }
  //print_table();
  for(int i=1;i<n;i++){
    for(int j=0;j<=n-i;j++){
      for(int k=j;k<j+i;k++){
	//cout << cards[j].first << ' ' << cards[k].second << ' ' << cards[k+1].first << ' ' << cards[j+i].second << endl;
        long long cost = cards[j].first * cards[k].second * cards[k+1].first * cards[j+i].second;
        dp[j][j+i] = min(dp[j][j+i],cost+dp[j][k]+dp[k+1][j+i]);
	//print_table();
      }
    }
  }
  cout<< dp[0][n-1] <<endl;
  return 0;
}