#line 2 "lib/base/base.hpp" #include using namespace std; #define rep(i,n) for(int (i)=0;(i)<(n);++(i)) #define rep3(i,m,n) for(int (i)=(m);(i)<(n);++(i)) #define endl '\n' #define all(v) (v).begin(),(v).end() using ll = long long; using vi = vector; using vvi = vector; using vll = vector; using vvll = vector; template using uset = unordered_set; template using umap = unordered_map; using pii = pair; using pll = pair; template using gprique = priority_queue, greater>; template using lprique = priority_queue, less>; const int mod998 = 998244353; const int mod107 = 1000000007; #line 2 "lib/utility/io.hpp" #define INT(...) \ int __VA_ARGS__; \ IN(__VA_ARGS__) #define LL(...) \ ll __VA_ARGS__; \ IN(__VA_ARGS__) #define STR(...) \ string __VA_ARGS__; \ IN(__VA_ARGS__) #define CHR(...) \ char __VA_ARGS__; \ IN(__VA_ARGS__) #define DBL(...) \ long double __VA_ARGS__; \ IN(__VA_ARGS__) #define VEC(type, name, size) \ vector name(size); \ read(name) #define VV(type, name, h, w) \ vector> name(h, vector(w)); \ read(name) void read(int &a) { cin >> a; } void read(long long &a) { cin >> a; } void read(char &a) { cin >> a; } void read(double &a) { cin >> a; } void read(long double &a) { cin >> a; } void read(string &a) { cin >> a; } template void read(pair &p) { read(p.first), read(p.second); } template void read(vector &a) {for(auto &i : a) read(i);} template void read(T &a) { cin >> a; } void IN() {} template void IN(Head &head, Tail &...tail) { read(head); IN(tail...); } template ostream& operator<<(ostream& os, const pair& A) { os << A.first << " " << A.second; return os; } template ostream& operator<<(ostream& os, const vector& A) { for (size_t i = 0; i < A.size(); i++) { if(i) os << " "; os << A[i]; } return os; } template ostream& operator<<(ostream& os, const map& A) { int count = 0; os << "{"; for(auto [t, u] : A){ if(count) os << ", "; os << t << ": " << u; count++; } os << "}"; return os; } template ostream& operator<<(ostream& os, const set& A) { for(auto it = A.begin(); it != A.end(); ++it){ if (it != A.begin()) os << " "; os << (*it); } return os; } template ostream& operator<<(ostream& os, const multiset& A) { size_t count = 0; T prev; os << "{"; bool is_null = true, is_first = true; for(auto it = A.begin(); it != A.end(); ++it){ if(it == A.begin()) { is_null = false; prev = (*it); count = 1; } if (prev != (*it)){ if(!is_first) os << ", "; is_first = false; os << prev << ": " << count; prev = (*it); count = 1; } else { ++count; } } if(!is_null){ if(!is_first) os << ", "; is_first = false; os << prev << ": " << count; } os << "}"; return os; } void print() { cout << "\n"; cout.flush(); } template void print(Head&& head, Tail&&... tail) { cout << head; if (sizeof...(Tail)) cout << " "; print(forward(tail)...); } void YES(bool t = 1) { print(t ? "YES" : "NO"); } void NO(bool t = 1) { YES(!t); } void Yes(bool t = 1) { print(t ? "Yes" : "No"); } void No(bool t = 1) { Yes(!t); } void yes(bool t = 1) { print(t ? "yes" : "no"); } void no(bool t = 1) { yes(!t); } #line 3 "main.cpp" bool DP[105][200200]; int main(){ cin.tie(nullptr); ios::sync_with_stdio(false); int l,n; IN(l,n); vi x(n); IN(x); int base = 100100; rep(j,n+2) rep3(i, base-l, base+l+1) DP[j][i] = false; DP[0][base] = true; rep(i,n+1){ // rep3(j,base-l,base+l+1){ // if (DP[i][j]) print(j-base,i); // } if (i==0){ rep3(j,base-l,base+l+1){ if (j - x[0]>=base-l) DP[i+1][j] = DP[i][j-x[0]]; } } else if (i==n){ rep3(j,base-l,base+l+1){ if (j + l - x[n-1]<=base+l) DP[i+1][j] = DP[i][j+l-x[n-1]]; } } else { rep3(j,base-l,base+l+1){ if (j + x[i]-x[i-1]<=base+l) DP[i+1][j] |= DP[i][j+x[i]-x[i-1]]; if (j - x[i]+x[i-1]>=base-l) DP[i+1][j] |= DP[i][j-x[i]+x[i-1]]; } } } int ans = l; // rep3(j,base-l,base+l+1){ // if (DP[n+1][j]) print(j-base, n); // } rep3(i,base-l,base+l+1){ if(DP[n+1][i]) { //print(i-base); ans = min(abs(base-i), ans); } } print(ans); }