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 <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int main(){
  int N, M;
  while(cin >> N >> M, N){
    int C[M];
    long long int INF = 10000000000LL;
    for(int i = 0; i < M; ++i)
      cin >> C[i];
    sort(C,C+M);
    int x[N];
    for(int i = 0; i < N; ++i)
      cin >> x[i];
    vector< vector<long long int> > dp(N, vector<long long int>(256,INF));
    for(int i = 0; i < M; ++i){
      int t = max(min(255,128+C[i]),0);
      dp[0][t] = (long long int)(x[0]-t)*(x[0]-t);
    }
    for(int i = 1; i < N; ++i){
      for(int j = 0; j < 256; ++j){
	if(dp[i-1][j] == INF) continue;
	for(int k = 0; k < M; ++k){
	  int t = max(min(255,j+C[k]),0);
	  dp[i][t] = min(dp[i][t], dp[i-1][j] + (long long int)(x[i]-t)*(x[i]-t));
	  if(j + C[k] > 255) break;
	}
      }
    }

    long long int ans = INF;
    for(int i = 0; i < 256; ++i){
      ans = min(ans, dp[N-1][i]);
    }
    cout << ans << endl;
  }
  return 0;
}

C++: #include <bits/stdc++.h>

using namespace std;
typedef long long int ll;
typedef pair<int, int> P;
typedef pair<ll, ll> Pll;
typedef vector<int> Vi;
typedef tuple<int, int, int> T;
#define FOR(i,s,x) for(int i=s;i<(int)(x);i++)
#define REP(i,x) FOR(i,0,x)
#define ALL(c) c.begin(), c.end()
#define MP make_pair
#define DUMP( x ) cerr << #x << " = " << ( x ) << endl
#define fst first
#define snd second

#define INF 1300500000

ll dp[2][16][2];
ll sample[20010];

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);

  while (1) {
    ll N, M, code[16];
    cin >> N >> M;
    if (N == 0 && M == 0) break;
    REP(i, M) cin >> code[i];
    REP(i, N) cin >> sample[i];

    REP(i, M) {
      dp[0][i][0] = 0;
      dp[0][i][1] = 128;
    }

    REP(i, N) {
      REP(j, M) {
	dp[(i+1)%2][j][0] = INF;
	REP(k, M) {
	  ll now = max(0LL, min(255LL, dp[i%2][k][1] + code[j]));
	  ll diff = sample[i] - now;
	  if (dp[i%2][k][0] + diff * diff < dp[(i+1)%2][j][0]) {
	    dp[(i+1)%2][j][0] = dp[i%2][k][0] + diff * diff;
	    dp[(i+1)%2][j][1] = now;
	  }
	}
      }
    }
    ll ans = INF;
    REP(i, M) ans = min(ans, dp[N%2][i][0]);
    cout << ans << endl;
  }

  return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define FOR(i,k,n) for(int i = (k); i < (n); i++)
#define REP(i,n) FOR(i,0,n)
#define INF 114514810
#define MOD 1000000007
#define ALL(a) (a).begin(),(a).end()
#define ms(m,v) memset(m,v,sizeof(m))
#define D10  fixed<<setprecision(10) 
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> P;
typedef long long ll;
struct edge{ int from, to, cost; };

int n, m;
int c[20]; int x[20005];
int memo[20005][300];

int dp(int y, int i)
{
	if (i == n) return 0;
	if (memo[i][y] != INF) return memo[i][y];
	int res = INF;
	REP(j, m)
	{
		int tmp = y;
		tmp += c[j];
		if (tmp > 255) tmp = 255;
		if (tmp < 0) tmp = 0;
		res = min(res, dp(tmp,i+1)+(tmp-x[i])*(tmp-x[i]));
	}
	return memo[i][y] = res;
}

int main()
{
	while (cin >> n >> m, n)
	{
		REP(i, 20005)REP(j, 300) memo[i][j] = INF;
		REP(i, m) cin >> c[i];
		REP(i, n) cin >> x[i];
		cout << dp(128, 0) << endl;
	}
	return 0;
}

C++: #include<iostream>
#include<cstdio>
#include<math.h>
#include<string>
#include<algorithm>
#include<functional>
#include<vector>
#include<set>
#include<map>
#define INF 999999999
#define EPS 1.0e-6

using namespace std;

typedef long long ll;
typedef pair<int, int> P;
typedef pair<int, P> PP;

int n, m;
int c[20];
ll x[21000];
ll d[1000][260];

int main()
{
	while (cin >> n >> m) {
		if (n == 0 && m == 0)return 0;
		for (int i = 0; i < m; i++) {
			cin >> c[i];
		}

		for (int i = 1; i <= n; i++) {
			cin >> x[i];
		}
		for (int i = 0; i <= n; i++) {
			for (int j = 0; j <= 256; j++) {
				d[i][j] = INF;
			}
		}
		d[0][128] = 0;

		for (int i = 0; i < n ; i++) {
			for (int j = 0; j <= 255; j++) {
				if (d[i][j] != INF) {
					for (int k = 0; k < m; k++) {
						int l = j + c[k];
						//cout << l << endl;
						if (l < 0)d[i + 1][0] = min(d[i + 1][0], x[i + 1] * x[i + 1]);
						else if (l > 255) {
							d[i + 1][255] = min(d[i + 1][256], d[i][j]+(x[i + 1] - 255)*(x[i + 1] - 255));
							//cout << d[i + 1][256] << endl;
						}
						else{
							d[i+1][l]= min(d[i + 1][l], d[i][j] + (x[i + 1] - l)*(x[i + 1] - l));
							//cout << i + 1 << " " << j << " " << l << " " << d[i + 1][l];
						}
						//cout << i + 1 << " " << j << " " << l << " " << d[i + 1][l]<<endl;
						
					}
				}
			}
		/*
			for (int j = 0; j <= 256; j++) {
				cout << i<< "j:" <<j<<" "<<d[i][j] << endl;

			}
			cout << endl;
			*/
			
		}
		ll ans = INF;
		for (int i = 0; i <= 255; i++) {
			//cout <<"d[n-1][i]:"<< d[n][i] << endl;
			ans = min(ans, d[n][i]);
		}
		cout << ans << endl;
	}

	return 0;
}