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 <cmath>
#define int long long
using namespace std;
int dp[20000][256];
signed main(){
  for(int n,m;cin>>n>>m,n;){
    int c[20000],x[20];
    for(int i=0;i<n;i++) fill(dp[i],dp[i]+256,(int)1e9);
    for(int i=0;i<m;i++) cin>>x[i];
    for(int i=0;i<n;i++) cin>>c[i];
    for(int i=0;i<m;i++) dp[0][x[i]+128]=(int)pow(x[i]+128-c[0],2);
    for(int i=1;i<n;i++){
      for(int j=0;j<256;j++){
	if(dp[i-1][j]!=(int)1e9){
	  for(int k=0;k<m;k++){
	    int num=j+x[k];
	    if(num<0) num=0;
	    if(num>255) num=255;
	    dp[i][num]=min(dp[i][num],dp[i-1][j]+(int)pow(c[i]-num,2));
	  }
	}
      }
    }
    /*
      for(int i=0;i<n;i++){
      for(int j=0;j<256;j++) cout<<(dp[i][j]!=1e9?dp[i][j]:-1)<<" ";
      cout<<endl;
      }*/
    int sm=1e9;
    for(int i=0;i<256;i++) sm=min(sm,dp[n-1][i]);
    cout<<sm<<endl;
  }
  return 0;
}

C++: #include <iostream>
#include <vector>
#include <cmath>
using namespace std;

unsigned int errorSum[2][256];
bool concerning[2][256];

short samples[20001];
int codeBook[16];

int main() {
	int n, m;
	while(true) {

		cin >> n >> m;

		if (n == 0 && m == 0) {
			break;
		}

		for (int i = 0; i < m; i++) {
			cin >> codeBook[i];
		}
		for (int i = 1; i <= n; i++) {
			cin >> samples[i];
		}

		for (int i = 0; i < 256; i++) {
			errorSum[0][i] = errorSum[1][i] = INT32_MAX;
			concerning[0][i] = concerning[1][i] = false;
		}
		concerning[0][128] = true;

		for (int i = 0; i < n; i++) {
			for (int j = 0; j < 256; j++) {
				if(concerning[i % 2][j]) {
					concerning[i % 2][j] = false;
					for (int k = 0; k < m; k++) {
						int next = min(max(0, j + codeBook[k]), 255);
						unsigned int error = (errorSum[i % 2][j] == INT32_MAX ?
								0 : errorSum[i % 2][j])
								+ (next - samples[i+1]) * (next - samples[i+1]);

						if(error <= errorSum[(i+1) % 2][next]) {
							concerning[(i+1) % 2][next] = true;
							errorSum[(i+1) % 2][next] = error;
						}
					}
				}
				errorSum[i % 2][j] = INT32_MAX;
			}
		}

		unsigned int minError = INT32_MAX;
		int lastIndex = n % 2;
		for (int i = 0; i < 256; i++) {
			if(concerning[lastIndex][i]
			        && errorSum[lastIndex][i] < minError) {
				minError = errorSum[lastIndex][i];
			}
		}
		cout << minError << endl;
	}
	return 0;
}

C++: #include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>

#define INF 255*255*20000
int n,m;
int c[17],x[30000];
int dp[20001][256];
int main(void){
	while(1){
		scanf("%d %d",&n,&m);
		if(n+m==0)break;
		for(int i=0;i<m;i++){
			scanf("%d",&c[i]);
		}
		for(int i=0;i<n;i++){
			scanf("%d",&x[i]);
		}
		memset(dp,-1,sizeof(dp));
		dp[0][128]=0;
		for(int i=0;i<n;i++){
			for(int j=0;j<256;j++){
				if(dp[i][j]>=0){
					for(int k=0;k<m;k++){
						int nx=j+c[k];
						if(nx<0)nx=0;
						if(nx>255)nx=255;
						int np=pow(nx-x[i],2);
						if(dp[i+1][nx]>np+dp[i][j] || dp[i+1][nx]==-1){
							dp[i+1][nx]=np+dp[i][j];
						}
					}
				}
			}
		}
		int res=INF;
		for(int i=0;i<256;i++){
			if(dp[n][i]>=0)res=std::min(res,dp[n][i]);
		}
		printf("%d\n",res);
	}
	return 0;
}

C++: #include <bits/stdc++.h>
#define LOOP(n) for (int _i = 0; _i < (n); _i++)
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define RREP(i, n) for (int i = (n); i >= 0; --i)
#define FOR(i, r, n) for (int i = (r); i < (n); ++i)
#define ALL(obj) begin(obj), end(obj)
using namespace std;
using ll = long long;
using ull = unsigned long long;
using P = pair<int, int>;
using Graph = vector<vector<int>>;

const int INF = 2100100100;
const int MOD = 1e9 + 7;

// 多次元 vector 生成
template <class T>
vector<T> make_vec(size_t a) {
    return vector<T>(a);
}
template <class T, class... Ts>
auto make_vec(size_t a, Ts... ts) {
    return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...));
}
template <class T>
bool chmax(T &a, const T &b) {
    if (a < b) {
        a = b;
        return 1;
    }
    return 0;
}
template <class T>
bool chmin(T &a, const T &b) {
    if (b < a) {
        a = b;
        return 1;
    }
    return 0;
}

int N, M, K, Q, W, H, R, C;
int dp[21000][300];

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

    vector<int> ans;
    while (true) {
        cin >> N >> M;
        if (N == 0 && M == 0) break;
        vector<int> C(M), x(N);
        REP(i, M) cin >> C.at(i);
        REP(i, N) cin >> x.at(i);

        REP(i, N + 2) {
            REP(j, 300) { dp[i][j] = INF; }
        }
        dp[0][128] = 0;

        REP(i, N) {
            REP(j, 256) {
                REP(k, M) {
                    int v = j + C[k];
                    if (v < 0) {
                        v = 0;
                    } else if (255 < v) {
                        v = 255;
                    }
                    chmin(dp[i + 1][v], dp[i][j] + (x[i] - v) * (x[i] - v));
                }
            }
        }
        int sum = INF;
        REP(i, 256) { chmin(sum, dp[N][i]); }
        ans.push_back(sum);
    }
    for (auto i : ans) {
        cout << i << endl;
    }
    return 0;
}