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 <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <climits>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define ALL(A)		(A).begin(),(A).end()
#define DUMP(A)    cout<<#A<<"="<<(A)<< endl
#define SIZE(A)    (int)((A).size())
using namespace std;
typedef long long ll;

int main(){
    int dp[100][100];
    int n;
    scanf("%d",&n);
    if(n==1){
        printf("0\n");
        return 0;
    }
    int ue[100],sita[100];
    for(int i=0;i<n;i++){
        scanf("%d%d",&ue[i],&sita[i]);
        dp[i][i]=0;
    }
    for(int kan=1;kan<=n-1;kan++){
        for(int i=0;i<n-kan;i++){
            int mi=INT_MAX;
            for(int j=i;j+1<=i+kan;j++){
                mi=min(mi,dp[i][j]+dp[j+1][i+kan]+ue[i]*sita[j]*ue[j+1]*sita[i+kan]);
            }
            dp[i][i+kan]=mi;
        }
    }
    printf("%d\n",dp[0][n-1]);
}

C++: #include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> P;

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

    scanf("%d", &n);
    for (int i = 0; i < n; i++) scanf("%d %d", a + i, b + i);

    int cost = 0;
    for (int i = 0; i < n - 1; i++){
        int a_min = min_element(a, a + n) - a;
        P ta = P(a[a_min], b[a_min]);
        a[a_min] = b[a_min] = INT_MAX;

        int b_min = min_element(b, b + n) - b;
        P tb = P(a[b_min], b[b_min]);
        a[b_min] = b[b_min] = INT_MAX;

        cost += ta.first * ta.second * tb.first * tb.second;
        a[a_min] = ta.first, b[a_min] = tb.second;
    }
    printf("%d\n", cost);

    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=1;k<=n-1;k++){
      for(int i=0;i+k<n;i++){
          for(int j=i;j<i+k;j++){
              ll cost=a[i]*b[j]*a[j+1]*b[i+k];
              dp[i][i+k]=min(dp[i][i+k],dp[i][j]+dp[j+1][i+k]+cost);
          }
      }
  }
  cout<<dp[0][n-1]<<endl;
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <numeric>

#define REP(i, a, b) for (int i = int(a); i < int(b); i++)
#ifdef _DEBUG_
#define dump(val) cerr << __LINE__ << ":\t" << #val << " = " << (val) << endl
#else
#define dump(val)
#endif

using namespace std;

typedef long long int ll;

template<typename T>
vector<T> make_v(size_t a, T b) {
    return vector<T>(a, b);
}
template<typename... Ts>
auto make_v(size_t a, Ts... ts) {
    return vector<decltype(make_v(ts...))>(a, make_v(ts...));
}

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int n;
    cin >> n;
    vector<ll> a(n), b(n);
    REP(i, 0, n) {
        cin >> a[i] >> b[i];
    }
    const ll inf = 1LL << 60;
    auto dp = make_v(n, n, inf);
    REP(i, 0, n) {
        dp[i][i] = 0;
    }
    REP(k, 1, n) {
        REP(i, 0, n - k) {
            int r = i + k;
            REP (j, i, r) {
                ll cost = dp[i][j] + dp[j + 1][r] + a[i] * b[j] * a[j + 1] * b[r];
                dp[i][r] = min(dp[i][r], cost);
            }
        }
    }

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

C++: #include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<map>

using namespace std;

int dp[1024][1024], num;
struct deck { int top, bottom; };
const int  INF = (1 << 20);

int main()
{

	cin >> num;
	vector<deck> mount(100);

	for(int i = 0; i < num; i++)
	{
		cin >> mount[i].top >> mount[i].bottom;
	}

	for(int i = 0; i < num; i++)
	{
		for(int j = 0; j < num; j++)
		{
			dp[i][j] = INF;
		}
	}

	for(int i = 0; i < num; i++)
	{
		dp[i][i] = 0;
	}

	for(int w = 1; w < num; w++)
	{
		for(int i = 0; i + w < num; i++)
		{
			int j = i + w;
			for(int k = i; k < j; k++)
			{
				dp[i][j] = min(dp[i][j], dp[i][k] + dp[k+1][j] + mount[i].top * mount[k].bottom * mount[k+1].top * mount[j].bottom );
			}
		}
	}
	cout << dp[0][num -1] << endl;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define REP(i,n,m) for(int i=n;i<m;i++)
#define rep(i,n) REP(i,0,n)
#define EREP(i,n,m) for(int i=n;i<=m;i++)
#define erep(i,n) EREP(i,0,n)

typedef pair<int,int> P;

unsigned int dp[101][101];

int main(void){
  int n; cin>>n;
  vector<P> v(n);
  rep(i,n) cin>>v[i].first>>v[i].second;

  erep(i,n){
    erep(j,n) dp[i][j] = INT_MAX;
    dp[i][i] = 0;
  }

  REP(i,1,n){ //???????±????
    EREP(j,0,n-i){ //????§???????????±????
      REP(k,j,j+i){ //????????????????±????
        int cost = v[j].first * v[k].second * v[k+1].first * v[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;

  return 0;
}

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

using namespace std;

typedef long long ll;
const ll INF = (1LL<<50);
const int MAX = 101;

ll dp[MAX][MAX];
int T[MAX],B[MAX];

int main(){

  int n;
  cin >> n;
  for(int i = 0; i < n; i++) cin >> T[i] >> B[i];
 
  fill(dp[0],dp[0]+MAX*MAX,INF);
  for(int i = 0; i < MAX; i++) dp[i][i] = 0;

  for(int i = 0; i <= n; i++)
    for(int j = i+1; j <= n; j++)
      for(int k = i+1; k <= j; k++)
	dp[i][j] = min(dp[i][j],dp[i][k-1]+dp[k][j]+B[i]*T[k-1]*B[k]*T[j]);
  
  cout << dp[0][n] << endl;
  return 0;
}

C++: #include <iostream>

using namespace std;

#define MN 100
#define INF (1<<28)

int n;
int P[MN][2];
int main() {
    cin >> n;
    for (int i = 0; i < n; i++) {
        int a, b; cin >> a >> b;
        P[i][0] = a;
        P[i][1] = b;
    }
    int dp[n][n]; // from, to, cost
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            if (i == j) {
                dp[i][j] = 0;
            } else {
                dp[i][j] = INF;
            }
        }
    }
    for (int i = 0; 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] + P[j][0] * P[k][1] * P[k+1][0] * P[j+i][1]);
            }
        }
    }
    cout << dp[0][n-1] << endl;
    return 0;
}