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 "bits/stdc++.h"
using namespace std;
int C[16];
int x[20001];
int dp[20001][256];
const int INF = 1 << 30;
int main() {
	int N, M;
	while (cin >> N >> M, N) {
		for (int i = 0; i < M; i++) {
			cin >> C[i];
		}
		for (int i = 0; i < N; i++) {
			cin >> x[i];
		}
		for (int i = 0; i <= N; i++) {
			for (int j = 0; j < 256; j++) dp[i][j] = INF;
		}
		dp[0][128] = 0;
		for (int i = 0; i < N; i++) {
			for (int j = 0; j < 256; j++) {
				if (dp[i][j] == INF) continue;
				for (int k = 0; k < M; k++) {
					int nj = j + C[k];
					if (0 > nj) nj = 0;
					if (nj > 255) nj = 255;
					int tmp = (nj - x[i])*(nj - x[i]);
					if (dp[i][j] + tmp < dp[i+1][nj]) {
						dp[i+1][nj] = dp[i][j] + tmp;
					}
				}
			}
		}
		int ans = INF;
		for (int i = 0; i < 256; i++) {
			ans = min(ans, dp[N][i]);
		}
		cout << ans << endl;
	}
	
}

C++: #include<iostream>
using namespace std;
const int inf=1e8;
int memo[20000+1][256];

void hoge(int N, int M){

   int c[M];
   for(int i=0; i<M; i++) cin>> c[i];
   int x[N];
   for(int i=0; i<N; i++) cin>> x[i];
   for(int i=0; i<N+1; i++){
      for(int j=0; j<256; j++){
         memo[i][j]=inf;
      }
   }

   memo[0][128]=0;
   for(int i=0; i<N; i++){
      for(int j=0; j<256; j++){
         if(memo[i][j]==inf) continue;
         for(int k=0; k<M; k++){
            int p=max(0, min(255, j+c[k]));
            memo[i+1][p]=min(memo[i][j]+(p-x[i])*(p-x[i]), memo[i+1][p]);
         }
      }
   }
   int mi=1e8;
   for(int j=0; j<256; j++) mi=min(mi, memo[N][j]);
   cout<< mi<< endl;

}


int main(){

   while(1){
      int N, M;
      cin>> N>> M;
      if(!(N||M)) break;
      hoge(N, M);
   }
   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;

const long long INF = 1e10;

int main() {
    int N, M;
    while (cin >> N >> M, N) {
        vector<int> C(M);
        for (int i=0; i<M; ++i) cin >> C[i];
        vector<int> x(N);
        for (int i=0; i<N; ++i) cin >> x[i];

        vector<long long> cost(256, INF);
        cost[128] = 0;
        for (int i=0; i<N; ++i) {
            vector<long long> cost2(256, INF);
            for (int cd: C) {
                for (int k=0; k<256; ++k) {
                    int kk = max(min(k+cd, 255), 0);
                    cost2[kk] = min(cost2[kk], cost[k] + (kk - x[i]) * (kk - x[i]));
                }
            }
            cost = cost2;
        }

        long long ans = *min_element(cost.begin(), cost.end());
        cout << ans << endl;
    }

    return 0;
}

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

using namespace std;

int N, M;
int C[17], x[20001];
int dp[256][20001];

int rec(int now, int cnt) {
  if ( cnt == N ) return 0;
  if ( dp[now][cnt] != -1 ) return dp[now][cnt];

  int ret = 1e8;
  for ( int i = 0; i < M; i++ ) {
    int tmp = max(0, min(255, now + C[i]));
    ret = min(ret, rec(tmp, cnt + 1) + (x[cnt] - tmp) * (x[cnt] - tmp));
  }

  return dp[now][cnt] = ret;
}

int main()
{
  while ( cin >> N >> M, N | M ) {
    for ( int i = 0; i < M; i++ ) cin >> C[i];
    for ( int i = 0; i < N; i++ ) cin >> x[i];
    
    fill_n(*dp, 256 * 20001, -1);
    
    cout << rec(128, 0) << endl;
  }
}