import java.util.Scanner; import java.util.Arrays; public class Main { public static void main(String args[]){ Scanner stdIn = new Scanner(System.in); int n = stdIn.nextInt(); //長さ int q = stdIn.nextInt(); //クエリ数 int seisu[] = new int[n]; for(int i = 0; i < n; i++){ seisu[i] = stdIn.nextInt(); } for(int i = 0; i < q; i++){ int l = stdIn.nextInt(); int r = stdIn.nextInt(); int use[] = new int[r - l + 1]; int index = 0; int sum = 0; for(int j = l - 1; j < r; j++){ use[index] = seisu[j]; sum += seisu[j]; index++; } Arrays.sort(use); int median = 0; if((r - l + 1) % 2 == 0){ median = sum / ((r - l + 1) / 2); }else{ median = use[(r - l + 1) / 2]; } int min = 0; for(int j = l - 1; j < r; j++){ if(median - seisu[j] >= 0){ min += median - seisu[j]; }else{ min += seisu[j] - median; } } System.out.println(min); } } }