fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. #define FAST ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
  4. /*bool is_prime(long int n)
  5. {
  6.   if (n == 1)
  7.   return false;
  8.   int i=2;
  9.   while (i*i <= n)
  10.   {
  11.   if (n % i == 0)
  12.   return false;
  13.   i += 1;
  14.   }
  15.   return true;
  16. }*/
  17. int main ()
  18. {
  19. FAST;
  20. //freopen("rev.in","r",stdin);
  21. int n , q;
  22. cin>>n>>q;
  23. vector<int>num(n);
  24. for(int I=0 ; I<n ; I++)
  25. cin>>num[I];
  26. sort(num.begin(),num.end());
  27. vector<int>temp(n);
  28. int I=0 ,j;
  29. for(; I<n/2 ; I++)
  30. temp[(n-1)-I]=num[I];
  31. j=I;
  32. for(; I<n ; I++)
  33. temp[I-j]=num[I];
  34. //prefix sum on temp
  35. vector<long long int >pr(n+1);
  36. pr[0]=0;
  37. for(int I=1 ; I<pr.size() ; I++)
  38. pr[I]=pr[I-1]+temp[I-1];
  39. long long int ms=0;
  40. while(q--)
  41. {
  42. int l ,r;
  43. cin>>l>>r;
  44. ms+=(pr[r]-pr[l-1]);
  45. }
  46. cout<<ms;
  47. return 0;
  48. }
Success #stdin #stdout 0.01s 5556KB
stdin
5 3
5 2 4 1 3
1 5
2 3
2 3
stdout
33