daily pastebin goal
36%
SHARE
TWEET

Untitled

a guest Feb 24th, 2013 1,945 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <vector>
  2. #include <list>
  3. #include <map>
  4. #include <set>
  5. #include <deque>
  6. #include <stack>
  7. #include <queue>
  8. #include <algorithm>
  9. #include <sstream>
  10. #include <iostream>
  11. #include <iomanip>
  12. #include <cstdio>
  13. #include <cmath>
  14. #include <cstdlib>
  15. #include <memory.h>
  16. #include <ctime>
  17.  
  18. using namespace std;
  19.  
  20. #define ABS(a) ((a>0)?a:-(a))
  21. #define MIN(a,b) ((a<b)?(a):(b))
  22. #define MAX(a,b) ((a<b)?(b):(a))
  23. #define FOR(i,a,n) for (int i=(a);i<(n);++i)
  24. #define FI(i,n) for (int i=0; i<(n); ++i)
  25. #define pnt pair <int, int>
  26. #define mp make_pair
  27. #define PI 3.14159265358979
  28. #define MEMS(a,b) memset(a,b,sizeof(a))
  29. #define LL long long
  30. #define U unsigned
  31.  
  32. int a[200100];
  33. int val[200100];
  34. int b[200100];
  35. int main()
  36. {
  37.         int n,q;
  38.         scanf("%d%d",&n,&q);
  39.         FOR(i,0,n)
  40.                 scanf("%d",&a[i]);
  41.         sort(a,a+n);
  42.         FOR(i,0,q)
  43.         {
  44.                 int l,r;
  45.                 scanf("%d%d",&l,&r);
  46.                 l--;
  47.                 r--;
  48.                 val[l]++;
  49.                 if (r<n-1)
  50.                         val[r+1]--;
  51.         }
  52.         int v=0;
  53.         FOR(i,0,n)
  54.         {
  55.                 v+=val[i];
  56.                 b[i]=v;
  57.         }
  58.         sort(b,b+n);
  59.         LL res=0;
  60.         FOR(i,0,n)
  61.                 res+=(b[i]*1ll*a[i]);
  62.         cout<<res<<endl;
  63.     return 0;
  64. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top