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 <fstream>
#include <iomanip>
#include <utility>
#include <cmath>
#include <random>
#include <vector>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <string>
#include <algorithm>
using namespace std;

#define rep(i,n) for(int i = 0; i<n; ++i)
#define REP(i,n) for(int i = 1; i<=n; ++i)
#define all(x) begin(x),end(x)
typedef long long ll;
typedef pair<int,int> ip;
typedef pair<ll,ll> lp;
const int inf = 1001001000;
const ll INF = 1LL<<60;
const int MOD = (int)1e9 + 7;

template<class T> inline bool chmin(T& a, T b) {if (a > b) { a = b; return true; } return false;}
template<class T> inline bool chmax(T& a, T b) {if (a < b) { a = b; return true; } return false;}

int point(int base, int sub){
	int ans = base+sub;
	if(ans>255)return 255;
	else if(ans <0)return 0;
	else return ans;
}

int x2(int a, int b){
	return (a-b)*(a-b);
}

int main(){
	int N,M;
	ofstream outputfile("ans.txt");
	while(true){
		cin>>N>>M;
		if(!N && !M)break;
		vector<int> C(M);
		vector<int> Y(N);
		rep(i,M)cin>>C[i];
		rep(i,N)cin>>Y[i];
		vector<vector<int>> dp(N, vector<int>(256,inf));
		rep(i,M){
			int p = point(128, C[i]);
			dp[0][p] = x2(p, Y[0]);
		}
		for(int i = 0; i<N-1; ++i){
			rep(j,256){
				if(dp[i][j] == inf)continue;
				// printf("[%d] -> %d\n", j, dp[i][j]);
				// cout<<"----"<<j<<"----"<<endl;
				rep(k,M){
					int p = point(j, C[k]);
					// cout<<p<<endl;
					chmin(dp[i+1][p], dp[i][j] + x2(p,Y[i+1]));
				}
			}
		}
		int ans = inf;
		rep(i,256)chmin(ans, dp[N-1][i]);
		// cout<<"---------"<<endl;
		cout<<ans<<endl;
		// cout<<"---------"<<endl;
		outputfile<<ans<<'\n';
	}
	outputfile.close();
	return 0;
}

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

using namespace std;

const int INF = 1<<30;

int n, m, data[16], dp[2][256], in;

main(){
  while(1){
    scanf("%d%d", &n, &m);
    if(!n && !m) break;
    for(int i=0;i<m;i++) scanf("%d", &data[i]);
    fill(dp[0], dp[2], INF);
    dp[0][128] = 0;
    for(int i=0;i<n;i++){
      for(int j=0;j<256;j++) dp[(i+1)%2][j] = INF;
      scanf("%d", &in);
      for(int j=0;j<256;j++){
        if(dp[i%2][j] == INF) continue;
        for(int k=0;k<m;k++){
          int next = j + data[k];
          if(next < 0) next = 0;
          if(next > 255) next = 255;
          dp[(i+1)%2][next] = min(dp[(i+1)%2][next], abs(in-(next)) * abs(in-(next)) + dp[i%2][j]);
        }
      }
    }
    int ans = INF;
    for(int i=0;i<256;i++) ans = min(ans, dp[n%2][i]);
    printf("%d\n", ans);
  }
}

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

using namespace std;

int main() {
    while(true) {
        int n, m; cin >> n >> m;
        if(n == 0 && m == 0) break;

        vector<int> code_block;
        vector<int> pulse_array;
        vector<vector<int>> dp;
        for(int i = 0; i < m; i++) {
            int tmp; cin >> tmp;
            code_block.push_back(tmp);
        }
        for(int i = 0; i < n; i++) {
            int tmp; cin >> tmp;
            pulse_array.push_back(tmp);
            dp.emplace_back(256, INT_MAX);
        }
        int p = 128;
        for(int code : code_block) {
            int c = p + code;
            if(c > 255) c = 255;
            else if(c < 0) c = 0;
            int diff = c - pulse_array[0];
            dp[0][c] = diff*diff;
        }
        for(int i = 1; i < n; i++) {
            for(p = 0; p < 256; p++) {
                if(dp[i-1][p] == INT_MAX) continue;
                for(int code : code_block) {
                    int c = p + code;
                    if(c > 255) c = 255;
                    else if(c < 0) c = 0;
                    int diff = c - pulse_array[i];
                    if(dp[i][c] > diff*diff + dp[i-1][p]) {
                        dp[i][c] = diff*diff + dp[i-1][p];
                    }
                }
            }
        }
        cout << *min_element(dp[n-1].begin(), dp[n-1].end()) << endl;
    }
    return 0;
}

// [Problem] Differential Pulse Code Modulation
// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2199&lang=jp

C++: #include <bits/stdc++.h>
#define inf 2000000000
using namespace std;

int n, m;
int c[20] = {0};
int x[20005] = {0};
// dp[i][j][k] x[i], use c[j], k:0 = nownum, 1 = sum
int dp[20005][20][2] = {0};

int solve();

int main() {
  int i, j, k;
  while(1) {
    cin >> n >> m;
    if(n + m == 0) break;
    for(i = 0; i < m; ++i) cin >> c[i];
    for(i = 0; i < n; ++i) cin >> x[i];
    for(i = 0; i < m; ++i)
      for(j = 0; j < n; ++j)
        for(k = 0; k < 2; ++k) dp[j][i][k] = inf;
    cout << solve() << endl;
  }
  return 0;
}

int solve() {
  int i, j, k, ans = inf;
  for(i = 0; i < n; ++i) {
    for(j = 0; j < m; ++j) {
      for(k = 0; k < m; ++k) {
        int now = 128, bf = 0;
        if(i != 0) {
          now = dp[i - 1][k][0];
          bf = dp[i - 1][k][1];
        }
        now += c[j];
        now = min(now, 255);
        now = max(now, 0);
        bf += (now - x[i]) * (now - x[i]);
        if(dp[i][j][1] > bf) {
          dp[i][j][1] = bf;
          dp[i][j][0] = now;
        }
        if(i == n - 1) ans = min(ans, dp[i][j][1]);
      }
    }
  }
  return ans;
}