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>

using namespace std;
#define INF (1<<29)
int n, m, x;
int c[20];
int dp[21600][256];
bool solve(){
	cin >> n >> m;
	if(n == 0 && m == 0)return false;
	for(int i = 0;i < m;i++){
		cin >> c[i];
	}
	for(int i = 0;i < 21600;i++){
		for(int j = 0;j < 256;j++){
			dp[i][j] = INF;
		}
	}
	dp[0][128] = 0;
	for(int i = 0;i < n;i++){
		cin >> x;
		for(int j = 0;j < 256;j++){
			for(int k = 0;k < m;k++){
				int nxt = max(0, min(255, j + c[k]));
				dp[i+1][nxt] = min(dp[i+1][nxt], dp[i][j] + (x - nxt)*(x - nxt));
			}
		}
	}
	int res = INF;
	for(int i = 0;i < 256;i++)res = min(res, dp[n][i]);
	cout << res << endl;
	return true;
}

int main(){
	while(solve());
	return 0;
}

C++: #include <bits/stdc++.h>
#define rep(i,n) for(int i=0; i < (n);++i)
using namespace std;
template <class _t> inline bool chmax (_t& a,_t b){if(a<b){a=b;return true;}return false;} template <class _t> inline bool chmin (_t& a,_t b){if(a>b){a=b;return true;}return false;}

const long long inf = 1ll << 60;
const long long mod = 1000000007;

int dx[4] = {1,0,-1,0};int dy[4] = {0,-1,0,1};

int main() {
    while(1){
	int n,m;cin >>n>> m;
	if(n==0 && m==0) return 0;
	vector<long long> C(m),v(n);
	for(int i=0;i<m;i++) cin >> C[i];
	for(int i=0;i<n;i++) cin >> v[i];
	vector<vector<long long> > dp(n+1,vector<long long>(256));
	rep(i,n+1)rep(j,256) dp[i][j] = inf;
	dp[0][128] = 0;
	for(int i=0;i<n;i++){
	    for(int j=0;j<=255;j++){
		for(int k=0;k<C.size();k++){
		    if(dp[i][j] == inf) continue;
		    if((j+C[k])<0){
			chmin(dp[i+1][0],dp[i][j]+v[i]*v[i]);
		    }else if((j+C[k])<256){ chmin(dp[i+1][j+C[k]],dp[i][j]+(v[i]-(j+C[k]))*(v[i]-(j+C[k])));
			/*
			if(i==0){
			cout << "j=" <<j << endl;
			cout << "dp[i][j]=" << dp[i][j] << endl;
			cout << "dp[i][j]+(v[i]-(j+C[k]))*(v[i]-(j+C[k])) =" << dp[i][j]+(v[i]-(j+C[k]))*(v[i]-(j+C[k])) << endl;
		    
			}
			*/
		    }else chmin(dp[i+1][255],dp[i][j]+(v[i]-255)*(v[i]-255));
		}	
	    }
	}
	/*
	for(int i=0;i<=n;i++){
	    for(int j=0;j<=255;j++){
		cout << dp[2][j] << " " ;
	    }
	    cout << endl;
	}
	*/
	long long  ans = inf;
	for(int i=0;i<256;i++) chmin(ans,dp[n][i]);
	cout << ans << endl;
    }
    return 0;
}

C++: #include <stdio.h>

int a[20001][256];

int main(void){

int i,j,k;
int n,m,dummy;
int min = 2000000000;
int c[17];
int x[20001];



while(1){
scanf("%d %d",&n,&m);
if(n == 0 && m == 0){
return 0;
}

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

for(i = 0; i < n + 1; i++){
for(j = 0; j < 255; j++){
a[i][j] = -1;
}
}

a[0][128] = 0;

for(i = 0; i < n; i++){
for(j = 0; j < 255; j++){
if(a[i][j] != -1){
for(k = 0; k < m; k++){
dummy = j + c[k];
if(dummy < 0){
dummy = 0;
}else if(dummy > 255){
dummy = 255;
}
if(a[i+1][dummy] = a[i][j] + (dummy - x[i])*(dummy - x[i])){
a[i+1][dummy] = a[i][j] + (dummy - x[i])*(dummy - x[i]);
}
}
}
}
}

for(j = 0; j < 255; j++){
if(a[n
][j] > -1){
if(min > a[n][j]){
min = a[n][j];
}
}
}

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

}
return 0;

}

C++: #include <iostream>
#include <algorithm>
#include <vector>
#include <utility>
#include <queue>

int solve(int N, int M) {
  std::vector<int> data(N);
  std::vector<int> codes(M);
  for (int i = 0; i < M; i++) {
    std::cin >> codes[i];
  }
  for (int i = 0; i < N; i++) {
    std::cin >> data[i];
  }

  struct Traverse {
    int n;
    int y;
    int diff;
  };
  auto compare = [](Traverse const& lhs, Traverse const& rhs) {
    return lhs.diff > rhs.diff;
  };
  std::priority_queue<Traverse, std::vector<Traverse>, decltype(compare)> queue(compare);
  queue.push(Traverse{0, 128, 0});

  while(!queue.empty()) {
    auto node = queue.top();
    queue.pop();
    if (node.n == N) {
      return node.diff;
    }
    for (auto code : codes) {
      int new_y = node.y + code;
      new_y = new_y < 0 ? 0 : new_y > 255 ? 255 : new_y;
      int diff = (new_y - data[node.n]) * (new_y - data[node.n]);
      queue.push(Traverse{node.n + 1, new_y, node.diff + diff});
    }
  }
}

int main() {
  int N, M;
  while (std::cin >> N >> M) {
    if (N == 0 && M == 0) {
      break;
    }
    std::cout << solve(N, M) << std::endl;
  }
}

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[y][i] != INF) return memo[y][i];
	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[y][i] = 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;
}