Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- void quickSort(int* array, int low, int high)
- {
- int i = low;
- int j = high;
- int pivot = array[(i + j) / 2];
- int temp;
- while (i <= j) {
- while (array[i] < pivot)
- i++;
- while (array[j] > pivot)
- j--;
- if (i <= j) {
- temp = array[i];
- array[i] = array[j];
- array[j] = temp;
- i++;
- j--;
- }
- }
- if (j > low)
- quickSort(array, low, j);
- if (i < high)
- quickSort(array, i, high);
- }
- int main()
- {
- int n, R, L, max_sum, min_sum, curr_sum;
- std::cin >> n;
- int a[n];
- for (int i = 0; i < n; i++)
- std::cin >> a[i];
- quickSort(a, 0, n);
- R = 0;
- max_sum = a[0];
- curr_sum = a[0];
- for (L = 0; L < n; L++) {
- if (L >= (n - 2)) {
- min_sum = a[L];
- }
- else
- min_sum = a[L] + a[L + 1];
- while ((R < (n - 1)) && (a[R + 1] <= min_sum)) {
- R += 1;
- curr_sum += a[R];
- }
- if (curr_sum > max_sum)
- max_sum = curr_sum;
- curr_sum -= a[L];
- }
- std::cout << max_sum;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment