Go: package main

import (
	"bufio"
	_ "container/heap"
	"fmt"
	"os"
	_ "sort"
	"strconv"
)

// I/O
type Scanner struct {
	sc *bufio.Scanner
}

func NewScanner() *Scanner {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	sc.Buffer(make([]byte, 1024), int(1e+9))
	return &Scanner{sc}
}

func (s *Scanner) nextStr() string {
	s.sc.Scan()
	return s.sc.Text()
}

func (s *Scanner) nextInt() int {
	i, e := strconv.Atoi(s.nextStr())
	if e != nil {
		panic(e)
	}
	return i
}

func (s *Scanner) nextFloat() float64 {
	f, e := strconv.ParseFloat(s.nextStr(), 64)
	if e != nil {
		panic(e)
	}
	return f
}

func (s *Scanner) nextRuneSlice() []rune {
	return []rune(s.nextStr())
}

func (s *Scanner) nextIntSlice(n int) []int {
	res := make([]int, n)
	for i := 0; i < n; i++ {
		res[i] = s.nextInt()
	}
	return res
}

func (s *Scanner) nextFloatSlice(n int) []float64 {
	res := make([]float64, n)
	for i := 0; i < n; i++ {
		res[i] = s.nextFloat()
	}
	return res
}

// Arithmetic
func max(nums ...int) int {
	m := nums[0]
	for _, i := range nums {
		if m < i {
			m = i
		}
	}
	return m
}

func min(nums ...int) int {
	m := nums[0]
	for _, i := range nums {
		if m > i {
			m = i
		}
	}
	return m
}

func abs(x int) int {
	if x > 0 {
		return x
	}
	return -x
}

func pow(x, y int) int {
	res := 1
	for i := 0; i < y; i++ {
		res *= x
	}
	return res
}

func ceil(a, b int) int {
	if a%b == 0 {
		return a / b
	} else {
		return a/b + 1
	}
}

func gcd(a, b int) int {
	if b == 0 {
		return a
	}
	return gcd(b, a%b)
}

func lcm(a, b int) int {
	return a / gcd(a, b) * b
}

// Sort
type RuneSlice []rune

func (a RuneSlice) Len() int           { return len(a) }
func (a RuneSlice) Less(i, j int) bool { return a[i] < a[j] }
func (a RuneSlice) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }

// Main
const MOD = int(1e+9) + 7
const INF = 1 << 60

func main() {
	sc := NewScanner()
	wtr := bufio.NewWriter(os.Stdout)
	defer wtr.Flush()
	for true {
		N, M := sc.nextInt(), sc.nextInt()
		if N == 0 && M == 0 {
			break
		}
		C := sc.nextIntSlice(M)
		X := sc.nextIntSlice(N)
		dp := make([][256]int, N+1)
		for i := 0; i < N+1; i++ {
			for j := 0; j < 256; j++ {
				dp[i][j] = INF
			}
		}
		dp[0][128] = 0
		for i := 0; i < N; i++ {
			for j := 0; j < 256; j++ {
				if dp[i][j] == INF {
					continue
				}
				for k := 0; k < M; k++ {
					nex := j + C[k]
					if nex < 0 {
						nex = 0
					}
					if nex > 255 {
						nex = 255
					}
					dp[i+1][nex] = min(dp[i+1][nex], dp[i][j]+pow(X[i]-nex, 2))
				}
			}
		}
		ans := INF
		for i := 0; i < 256; i++ {
			ans = min(ans, dp[N][i])
		}
		fmt.Fprintln(wtr, ans)
	}
}

C++: #include<iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<cctype>
#include<math.h>
#include<string>
#include<string.h>
#include<stack>
#include<queue>
#include<vector>
#include<utility>
#include<set>
#include<map>
#include<stdlib.h>
#include<iomanip>

using namespace std;

#define ll long long
#define ld long double
#define EPS 0.0000000001
#define INF 1e9
#define MOD 1000000007
#define rep(i,n) for(i=0;i<(n);i++)
#define loop(i,a,n) for(i=a;i<(n);i++)
#define all(in) in.begin(),in.end()
#define shosu(x) fixed<<setprecision(x)

typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int,int> pii;

ll dp[20005][256];

int main(void) {
    int i,j;
    int n, m;
    while(cin >> n >> m, n+m){
        vi c(m),x(n);
        rep(i,m) cin >> c[i];
        rep(i,n) cin >> x[i];
        rep(i,n+1)rep(j,256)
            dp[i][j] = (ll)INF*INF;
        dp[0][128] = 0;
        int k;
        rep(i,n)rep(j,256)rep(k,m){
            ll t = j + c[k];
            if(t < 0) t = 0;
            if(t > 255) t = 255;
            dp[i+1][t] = min(dp[i+1][t], dp[i][j] + (t-x[i])*(t-x[i]));
        }
        ll ans = (ll)INF*INF;
        rep(i,256)
            ans = min(ans, dp[n][i]);
        cout << ans << endl;
    }
}

C++: /*
    Problem 45
    https://onlinejudge.u-aizu.ac.jp/problems/2199
*/
#include <bits/stdc++.h>
using namespace std;
/* typedef */
typedef long long ll;
typedef pair<int, int> pii;
/* constant */
const ll LINF = 1LL << 50;
const int YMAX = 256;
const int YMIN = 0;
const int NMAX = 20004;
const int MMAX = 20;
/* global variables */
vector<ll> input(NMAX);
vector<int> code(MMAX);
vector<vector<ll>> dp(NMAX, vector<ll>(YMAX, LINF));
/* function */
void calc(int N, int M) {
    for (int i = 0; i < M; i++) cin >> code[i];
    for (int i = 0; i < N; i++) cin >> input[i];

    // init
    for (int i = 0; i <= N; i++) for (int j = YMIN; j < YMAX; j++) dp[i][j] = LINF;
    dp[0][128] = 0;  // y0 = 128
    // calc
    // dp[i][j]
    // iこ目のinpuまで圧縮して, yi == jである時の最小値
    for (int i = 1; i <= N; i++) {  // 1 ~ Nこのinputを圧縮
        for (int ypre = 0; ypre < YMAX; ypre++) {  // 前のyは
            for (int j = 0; j < M; j++) {  // 何番目のcode
                // 次のyi
                ll yi = max(0, min(YMAX - 1, ypre + code[j]));  // 0 <= yi <= 255
                ll cost = pow(yi - input[i-1], 2);
                // dp[i][yi] <- dp[i-1][ypre]の遷移
                dp[i][yi] = min(dp[i][yi], dp[i-1][ypre] + cost);
            }
        }
    }

    ll ans = *min_element(dp[N].begin(), dp[N].end());
    cout << ans << '\n';
}
/* main */
int main(){
    while (true) {
        int N, M;
        cin >> N >> M;
        if (N + M == 0) return 0;
        calc(N, M);
    }
}

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

typedef long long ll;
const ll MOD = 1e9 + 7;
const long long INF = 1LL<<60;
const double PI=3.14159265358979323846;
const int NMAX=100005;
using namespace std;

ll dp[20001][256];

vector<int> c,x;

void solve(ll n, ll m){
    for(int i = 0; i <= n; i++){
        for(int j = 0; j <= 255; j++){
            dp[i][j] = INF;
        }
    }
    dp[0][128] = 0;
    for(int i = 0; i < n; i++){
        for(int j = 0; j < m; j++){
            for(int k = 0; k <= 255; k++){
                int cc = k+c[j];
                if(cc < 0) cc = 0;
                else if(cc > 255) cc = 255;
                dp[i+1][cc] = min(dp[i+1][cc], dp[i][k]+abs(x[i]-cc)*abs(x[i]-cc));
            }
        }
    }
    ll ans = INF;
    for(int i = 0; i <= 255; i++){
        ans = min(ans, dp[n][i]);
    }
    cout << ans << endl;
}

int main(){
    while(true){
        c.clear();
        x.clear();
        ll n, m;
        cin >> n >> m;
        if(n == 0 && m == 0) break;
        for(int i = 0; i < m; i++){
            int tmp;
            cin >> tmp;
            c.push_back(tmp);
        }
        for(int i = 0; i < n; i++){
            int tmp;
            cin >> tmp;
            x.push_back(tmp);
        }
        solve(n, m);
    }
    return 0;
}

C++: #include <bits/stdc++.h>
#define rep(i,a,n) for(int i=a;i<n;i++)
#define repb(i,a,b) for(int i=a;i>=b;i--)
#define all(a) a.begin(),a.end()
#define o(a) cout<<a<<endl
#define int long long
#define fi first
#define se second
using namespace std;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> pii;

int dp[20010][256];
const int INF=1e12;

signed main(){
   int n,m;
   	while(1){
	   	cin>>n>>m;
	   	if(n+m==0) break;
	   	vi c(m),x(n);
	   	rep(i,0,m) cin>>c[i];
	   	rep(i,0,n) cin>>x[i];
	   	rep(i,0,n+10){
	   		rep(j,0,256){
	   			dp[i][j]=INF;
	   		}
	    }
	    dp[0][128]=0;
	    rep(i,0,n){
	    	rep(j,0,256){
	    		if(dp[i][j]==INF) continue;
	    		rep(k,0,m){
	    			int tmp=j+c[k];
	    			tmp=max((int)0,tmp);
	    			tmp=min((int)255,tmp);
	    			int diff=pow(abs(tmp-x[i]),(int)2);
	    			dp[i+1][tmp]=min(dp[i+1][tmp],dp[i][j]+diff);
	    		}
	    	}
	    }
	    int MIN=INF;
	    rep(i,0,256){
	    	MIN=min(MIN,dp[n][i]);
	    }
	    cout<<MIN<<endl;
	}
}