#include #include #include using namespace std; #define ALL(a) (a).begin(), (a).end() int main() { int n, m; cin >> n >> m; vector neg_trash, pos_trash; for (auto i = 0; i < m; i++) { int d; cin >> d; if (d > 0) pos_trash.push_back(d); else if (d < 0) neg_trash.push_back(d); else n--; } sort(ALL(pos_trash)); sort(ALL(neg_trash), greater()); auto ans = (1 << 28); // 正の位置にあるゴミからいくつとるかで全探索 for (auto pos_num = 0; pos_num <= min(n, (int)pos_trash.size()); pos_num++) { auto neg_num = n - pos_num; if (neg_num > neg_trash.size()) continue; auto pos_end = pos_num > 0 ? pos_trash[pos_num - 1] : 0, neg_end = neg_num > 0 ? neg_trash[neg_num - 1] : 0; ans = min(ans, min(abs(pos_end), abs(neg_end)) + abs(pos_end - neg_end)); } cout << ans << endl; return 0; }