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<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;

int c[30],x[30000];
int dp[30000][256];

int sq(int x) {
    return x*x;
}
int add(int x, int y) {
    return max(0, min(255, x+y));
}
int main(void) {
    while(1) {
        int n,m;
        scanf("%d%d",&n,&m);
        if(!n) break;
        memset(dp, 0x6F, sizeof(dp));

        for(int i=0; i<m; i++)
            scanf("%d",&c[i]);
        for(int i=0; i<n; i++)
            scanf("%d",&x[i]);

        for(int i=0; i<256; i++)
            dp[0][i] = 0xFFFFFF;
        dp[0][128] = 0;

        for(int i=0; i<n; i++)
            for(int j=0; j<256; j++)
                for(int k=0; k<m; k++)
                    dp[i+1][add(j,c[k])] = min(dp[i+1][add(j,c[k])], dp[i][j] + sq(x[i]-add(j,c[k])));

        int res=0xFFFFFF;
        for(int j=0; j<256; j++)
            res = min(res, dp[n][j]);

        printf("%d\n", res);

    }
    return 0;
}

C++: #include "bits/stdc++.h"

using namespace std;

const long INF = 1e15;

void solve(void)
{
	int n, m;
	const int s = 256;
	while(cin >> n >> m)
	{
		if (!n && !m) break;
		vector<int> cs(m);
		for (int& c : cs) cin >> c;
		vector<long> dp(s, INF);
		dp[128] = 0;
		for (int i = 0; i < n; i++)
		{
			int x;
			cin >> x;
			vector<long> nx(s, INF);
			for (int j = 0; j < s; j++)
			{
				if (dp[j] == INF) continue;
				for (int c : cs)
				{
					int k = j + c;
					if (k < 0) k = 0;
					if (k >= s) k = s-1;
					nx[k] = min(nx[k], dp[j] + (x-k)*(x-k));
				}
			}
			swap(dp, nx);
		}
		cout << *min_element(dp.begin(), dp.end()) << '\n';
	}
}

int main()
{
	solve();
	//cout << "yui(*-v・)yui" << endl;
	return 0;
}

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

using namespace std;

inline int adj(int x) {
  return min(max(x, 0), 255);
}
#define SQ(x) ((x)*(x))
int main() {
  int N, M, INF=1<<28;
  while(cin >> N >> M && (N|M)) {
    int C[M];
    for(int i=0; i<M; i++) cin >> C[i];
    int dp[2][256]; fill(dp[0], dp[0]+512, INF);
    dp[128] = 0;
    for(int k=0; k<N; k++) {
      for(int i=0; i<256; i++) dp[(k+1)%2][i] = INF;
      int in; cin >> in;
      for(int i=0; i<256; i++) {
        if(dp[i]>=INF) continue;
        for(int j=0; j<M; j++) {
          dp[(k+1)%2][adj(i+C[j])] = min(dp[(k+1)%2][adj(i+C[j])], dp[k%2][i]+SQ(in-adj(i+C[j])));
        }
      }
    }
    cout << *min_element(dp[N%2], dp[(N%2)+1]) << endl;
  }
  
  return 0;
}

C++: #include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<string>
#include<sstream>
#include<map>
#include<cmath>
using namespace std;
#define rep(i,n) for(int i = 0;i < n;i++)
#define req(i,n) for(int i = 1;i <=n;i++)
#define rrep(i,n) for(int i = n -1;i >= 0;i--)
typedef long long ll;
typedef long double ld;
const ll MOD =  1000000007;
int n,m,x;
int main() {
    while (cin >> n >> m, n) {
        vector<int> c(m);
        vector<vector<int>> dp(256, vector<int>(n + 1, MOD));
        // dp[Ynの値][i番目のサンプル数]
        dp[128][0] = 0; // Y0 = 128
        rep(i, m) cin >> c[i];
        req(i, n) {// DPテーブルが0の時に処理を行ってしまうと、最小値になる
            cin >> x;
            rep(j, 256) {
                rep(k, m) {
                    int u = max(min(j + c[k],255),0);
                    dp[u][i] = min(dp[u][i],(x - u)*(x - u) + dp[j][i - 1]);
                }
            }
        }
        int ans = 1 << 30;
        rep(i, 256) ans = min(ans, dp[i][n]);
        cout << ans << endl;
    }
}

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

const int INF = 1 << 28;
int res;

vector<int> c;
vector<int> x;

void dfs (int y, int n, int sum) {
    if (res < sum) return;
    if (n == x.size()) {
        res = min(res, sum);
        return;
    }

    for (auto i : c) {
        int yn = y + i;
        if (yn > 255) yn = 255;
        if (yn < 0) yn = 0;
        dfs(yn, n + 1, sum + ((yn - x[n]) * (yn - x[n])));
    }
}

int main ()
{
    int n, m;
    while (cin >> n >> m, n || m) {
        res = INF;

        c.resize(m);
        x.resize(n);

        for (auto &i : c) cin >> i;
        for (auto &i : x) cin >> i;

        dfs(128, 0, 0);

        cout << res << endl;
    }

    return 0;
}

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

using namespace std;

//typedef long long lli;

const int INF = 1<<30;

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

main(){
  while(cin >> n >> m && (n|m)){
    for(int i=0;i<m;i++) cin >> 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;
      cin >> 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]);
    cout << ans << endl;
  }
}