#include<bits/stdc++.h>
#define INF 1e9
using namespace std;
int n, m, htB[100] = {10,10,12,13,16}, htG[100] = {6,7,9,10,11,12,17}, dp[100][100];
int solve(int idx1, int idx2){
if(idx1 == n) return 0;
if(idx2 == m) return INF;
if(dp[idx1][idx2] != -1) return dp[idx1][idx2];
int v1, v2;
//include current
v1 = solve(idx1 + 1, idx2 + 1) + abs(htB[idx1] - htG[idx2]);
//do not include current
v2 = solve(idx1, idx2 + 1);
return dp[idx1][idx2] = min(v1, v2);
}
int main(){
n = 5, m = 7;
sort(htB, htB+n);sort(htG, htG+m);
for(int i = 0;i < 100;i++) for(int j = 0;j < 100;j++) dp[i][j] = -1;
cout << solve(0, 0) << endl;
return 0;
}
I2luY2x1ZGU8Yml0cy9zdGRjKysuaD4KI2RlZmluZSBJTkYgMWU5Cgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKaW50IG4sIG0sIGh0QlsxMDBdID0gezEwLDEwLDEyLDEzLDE2fSwgaHRHWzEwMF0gPSB7Niw3LDksMTAsMTEsMTIsMTd9LCBkcFsxMDBdWzEwMF07CgppbnQgc29sdmUoaW50IGlkeDEsIGludCBpZHgyKXsKICAgIGlmKGlkeDEgPT0gbikgcmV0dXJuIDA7CiAgICBpZihpZHgyID09IG0pIHJldHVybiBJTkY7CiAgICAKICAgIGlmKGRwW2lkeDFdW2lkeDJdICE9IC0xKSByZXR1cm4gZHBbaWR4MV1baWR4Ml07CgogICAgaW50IHYxLCB2MjsKCiAgICAvL2luY2x1ZGUgY3VycmVudAogICAgdjEgPSBzb2x2ZShpZHgxICsgMSwgaWR4MiArIDEpICsgYWJzKGh0QltpZHgxXSAtIGh0R1tpZHgyXSk7CgogICAgLy9kbyBub3QgaW5jbHVkZSBjdXJyZW50CiAgICB2MiA9IHNvbHZlKGlkeDEsIGlkeDIgKyAxKTsKCiAgICByZXR1cm4gZHBbaWR4MV1baWR4Ml0gPSBtaW4odjEsIHYyKTsKfQoKCmludCBtYWluKCl7CgogICAgbiA9IDUsIG0gPSA3OwogICAgc29ydChodEIsIGh0QituKTtzb3J0KGh0RywgaHRHK20pOwogICAgZm9yKGludCBpID0gMDtpIDwgMTAwO2krKykgZm9yKGludCBqID0gMDtqIDwgMTAwO2orKykgZHBbaV1bal0gPSAtMTsKICAgIGNvdXQgPDwgc29sdmUoMCwgMCkgPDwgZW5kbDsKICAgIHJldHVybiAwOwp9