Advertisement
YEZAELP

PROG-1117: ภารกิจ (mission)

Oct 18th, 2020
109
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.57 KB | None
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4. using lli = long long;
  5. lli ar[100010];
  6. int n;
  7.  
  8. int main(){
  9.  
  10.     scanf("%d", &n);
  11.  
  12.     for(int i=1;i<=n;i++){
  13.         lli A, B;
  14.         scanf("%lld%lld", &A, &B);
  15.         ar[i] = B - 2*A;
  16.     }
  17.  
  18.     sort(ar+1, ar+n+1);
  19.  
  20.     lli mx = 0, sum = 0;
  21.     for(int i=1;i<=n;i++){
  22.         if(sum + ar[i] > sum - (2*i - 1))
  23.             sum += ar[i];
  24.         else
  25.             sum -= 2*i - 1;
  26.     }
  27.  
  28.     printf("%lld", sum);
  29.  
  30.     return 0;
  31. }
  32.  
  33. /*
  34. 1 = 1
  35. 2 = 4 = 1 + 3
  36. 3 = 9 = 1 + 3 + 5
  37. 4 = 16 = 1 + 3 + 5 + 7
  38. */
  39.  
Advertisement
RAW Paste Data Copied
Advertisement