fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. const int MX = 200005;
  5. int n;
  6. long long m, Arr[MX], tmp[MX];
  7.  
  8. void solve(){
  9. long long mx = 0;
  10. for(int i = 0; i<n; i++){
  11. int num = Arr[i];
  12. long long pre = tmp[num];
  13. if(i != 0) pre -= tmp[Arr[i-1]];
  14. long long ans = pre * (long long) num;
  15. mx = max(ans, mx);
  16.  
  17. for(int j = 2 * num; j < MX; j += num){
  18. long long diff = tmp[j] - tmp[j - num];
  19. if(diff > 0){
  20. int lo = lower_bound(Arr, Arr + n, j) - Arr;
  21. int hi = upper_bound(Arr, Arr + n, j) - Arr;
  22. ans += (long long)(hi - lo) * (long long) j;
  23. ans += (diff - (long long)(hi - lo)) * (long long)(j - num);
  24. }
  25. }
  26. mx = max(mx, ans);
  27. }
  28.  
  29. printf("%lld\n", mx);
  30. }
  31.  
  32. int main() {
  33. scanf("%d", &n);
  34. m = 0;
  35. for(int i = 0; i<n; i++){
  36. scanf("%lld", &Arr[i]);
  37. m = max(m, Arr[i]);
  38. tmp[Arr[i]]++;
  39. }
  40.  
  41. sort(Arr, Arr + n);
  42. for(int i = 1; i< MX; i++){
  43. tmp[i] += tmp[i-1];
  44. }
  45.  
  46. solve();
  47. return 0;
  48. }
Success #stdin #stdout 0s 6600KB
stdin
4
8 2 2 7
stdout
18