#include using namespace std::literals::string_literals; using i64 = long long; using std::cout; using std::endl; using std::cin; template std::vector make_v(size_t a){return std::vector(a);} template auto make_v(size_t a,Ts... ts){ return std::vector(ts...))>(a,make_v(ts...)); } int main() { // input int n, q; scanf("%d%d", &n, &q); assert(1 <= n and n <= 1e5); assert(1 <= q and q <= 1e5); std::vector a(n); for(int i = 0; i < n; i++) { scanf("%d", &a[i]); assert(-1e9 <= a[i] and a[i] <= 1e9); } std::vector L(q), R(q); for(int i = 0; i < q; i++) { scanf("%d%d", &L[i], &R[i]); assert(1 <= L[i] and L[i] <= n); assert(1 <= R[i] and R[i] <= n); assert(L[i] <= R[i]); L[i]--; } // solve for(int i = 0; i < q; i++) { std::vector vec(R[i] - L[i]); std::copy(begin(a) + L[i], begin(a) + R[i], begin(vec)); sort(begin(vec), end(vec)); i64 ans = 0; for(auto v: vec) ans += std::abs(v - vec[vec.size() >> 1]); printf("%lld\n", ans); } return 0; }