fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define ll long long
  4.  
  5. ll n,m,x,y,b[3000000],c=0,t;
  6. vector<pair<ll,ll>>a;
  7. int main() {
  8. cin>>n>>m;
  9. for(int i=1;i<=n;i++){
  10. cin>>x>>y;
  11. a.push_back(make_pair(x,y));
  12. }
  13. sort(a.begin(),a.end());
  14. for(int i=0;i<n;i++){
  15. if(a[i].second<a[i].first){
  16. b[c]=a[i].second;
  17. c++;
  18. }
  19. }
  20. sort(b,b+c);
  21. if(c!=0)t=2*a[n-1].first-b[0];
  22. for(int i=1;i<c;i++){
  23. t+=b[i]-b[i-1];
  24. }
  25. cout<<t+(m-b[c-1]);
  26. return 0;
  27. }
Success #stdin #stdout 0s 5288KB
stdin
2 853608075
100586680 226096596
177110870 354886264
stdout
853608075