Advertisement
Guest User

Untitled

a guest
Jan 7th, 2017
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>s
  2. #include <set>
  3. #include <cmath>
  4.  
  5. #define TASK ""
  6. #define ffor(a, n, i) for(int i = a; i < n; i++)
  7. #define bfor(a, n, i) for(int i = a; i >= n; i--)
  8. #define all(a) a.begin(), a.end()
  9. #define rall(a) a.rbegin(), a.rend()
  10.  
  11. typedef long long ll;
  12.  
  13. using namespace std;
  14.  
  15. int LL_INF = (long long)3 * 1e18 + 9;
  16. int INT_INF = 1 * 1e9 + 9;
  17.  
  18. set<int> intersection;
  19. int in[1000001], out[1000001];
  20. set<int> numbers[1000001];
  21. int arr[100000];
  22. int main()
  23. {
  24.     int n;
  25.     ll ans = 0;
  26.     cin >> n;
  27.  
  28.     for (int i = 0; i < n; i++)
  29.     {
  30.         int a;
  31.         scanf("%d", &a);
  32.         arr[i] = a;
  33.         for (int j = 1; j * j <= a; j++)
  34.         {
  35.             if (a%j == 0)
  36.             {
  37.                 int d1, d2;
  38.                 d1 = j;
  39.                 d2 = a / j;
  40.                 out[d1]++;
  41.                 numbers[d1].insert(i);
  42.                 if (d1 != d2)
  43.                 {
  44.                     out[d2]++;
  45.                     numbers[d2].insert(i);
  46.                 }
  47.             }
  48.         }
  49.     }
  50.     for (int i = 0; i < n; i++)
  51.     {
  52.         int ind = *numbers[1].begin();
  53.         int number = arr[ind];     
  54.         if (i != 0)
  55.         {
  56.             set<int>::iterator it = intersection.end();
  57.             it--;
  58.             int max_div = *it;
  59.             ind = *numbers[max_div].begin();
  60.             number = arr[ind];
  61.             ans += max_div;
  62.         }
  63.  
  64.         for (int j = 1; j * j <= number; j++)
  65.         {
  66.             if (number%j == 0)
  67.             {
  68.                 int d1 = j, d2 = number / j;
  69.                
  70.                 out[d1]--;
  71.                 in[d1]++;
  72.  
  73.                 if (out[d1] == 0)
  74.                     intersection.erase(d1);
  75.  
  76.                 if (in[d1] == 1 && out[d1] != 0)
  77.                     intersection.insert(d1);
  78.  
  79.                 numbers[d1].erase(ind);
  80.  
  81.                 if (d1 != d2)
  82.                 {
  83.                     out[d2]--;
  84.                     in[d2]++;
  85.  
  86.                     if (out[d2] == 0)
  87.                         intersection.erase(d2);
  88.  
  89.                     if (in[d2] == 1 && out[d2] != 0)
  90.                         intersection.insert(d2);
  91.  
  92.                     numbers[d2].erase(ind);
  93.                 }
  94.             }
  95.         }
  96.     }
  97.     cout << ans;
  98.     return 0;
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement