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 <iomanip>
#include <algorithm>
#include <string>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <stack>

#define INF (int)1e09

using std::cin;
using std::cout;
using std::endl;
using std::setprecision;
using std::fixed;
using std::pair;
using std::make_pair;
using std::min;
using std::max;
using std::string;
using std::vector;
using std::list;
using std::map;
using std::set; // insert erase find count
using std::queue;
using std::priority_queue;
using std::stack;

template <typename T> using Vec = vector<T>; // c++11
typedef pair<int, int> PII;
typedef pair<int, pair<int, int> > PIII;

int main(void) {
    while (1) {
        int n;
        int m;
        cin >> n >> m;
        if (n == 0 && m == 0) {
            break;
        }
        Vec<int> c(m);
        Vec<int> x(n + 1);
        for (int i = 0; i < m; i++) {
            cin >> c[i];
        }
        for (int j = 1; j <= n; j++) {
            cin >> x[j];
        }
        Vec<Vec<long long int> > dp(m, Vec<long long int>(n + 1, 0));

        Vec<int> now(m, 128);
        Vec<int> upd(m, 0);
        for (int j = 1; j <= n; j++) {
            for (int i = 0; i < m; i++) {
                for (int k = 0; k < m; k++) {
                    long long int sum = now[k] + c[i];
                    if (sum > 255) {
                        sum = 255;
                    } else if (sum < 0) {
                        sum = 0;
                    }

                    long long int div = x[j] - sum;
                    if (k == 0 || dp[i][j] > dp[k][j - 1] + div * div) {
                        dp[i][j] = dp[k][j - 1] + div * div;
                        upd[i] = sum;
                    }
                }
            }
            for (int i = 0; i < m; i++) {
                now[i] = upd[i];
            }
        }

        long long int ans;
        for (int i = 0; i < m; i++) {
            if (i == 0 || ans > dp[i][n]) {
                ans = dp[i][n];
            }
        }
        cout << ans << endl;
    }
    return 0;
}

C++: #include<iostream>
using namespace std;
#define INF (1<<28)
int N,M;
int t[20000];
int u[20000];
int dp[20000][256];

void init(){
  for(int i=0;i<20000;i++)
    for(int j=0;j<256;j++)
      dp[i][j]=INF;
}

int calc(int a,int b){
  if(a+b<0)return 0;
  if(a+b>=256)return 255;
  return a+b;
}

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

int main(){
  while(1){
    cin>>N>>M;
    if(N==0&&M==0)break;
    init();
    for(int i=0;i<M;i++)cin>>t[i];
    for(int i=0;i<N;i++)cin>>u[i];
    for(int i=0;i<M;i++)dp[0][calc(128,t[i])]=dist(calc(128,t[i]),u[0]);

    for(int i=0;i+1<N;i++){
      for(int j=0;j<256;j++){
	if(dp[i][j]==INF)continue;
	//cout<<i<<' '<<j<<' '<<dp[i][j]<<endl;
	for(int k=0;k<M;k++){
	  int A=calc(j,t[k]);
	  //cout<<A<<endl;
	  dp[i+1][A]=min(dp[i+1][A],dp[i][j]+dist(A,u[i+1]));
	  
	}
      }
    }
    int ans=INF;
    for(int i=0;i<256;i++)
      ans=min(ans,dp[N-1][i]);
    cout<<ans<<endl;
  }
  return 0;
}

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

using namespace std;

const int inf = 1e9;

int r(int a, int b) {
    if ((a+b) >= 255) return 255;
    else if ((a+b) <= 0) return 0;
    else return a+b;
}

int main(void){
    int n,m;
    while (cin>>n>>m && n) {
        vector<int> a(n);
        vector<int> code(m);
        for (int i = 0; i < m; i++) cin >> code[i];
        for (int i = 0; i < n; i++) cin >> a[i];
        
        vector<vector<int> > dp(256, vector<int>(n+1, inf) );
        dp[128][0] = 0;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < 256; j++) {
                if (dp[j][i] == inf) continue;
                
                int x, p;
                for (int k = 0; k < m; k++) {
                    x = r(j, code[k]);
                    p = (a[i]-x) * (a[i]-x);
                    dp[x][i+1] = min(dp[x][i+1], dp[j][i]+p);
                }
            }
        }
        int ans = inf;
        for (int i = 0; i < 256; i++) ans = min(ans, dp[i][n]);
        cout << ans << endl;
    }
}