Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // This file is a "Hello, world!" in C++ language by GCC for wandbox.
- #include <iostream>
- #include <algorithm>
- #include <vector>
- using namespace std;
- long long maxi (long long int a, long long b)
- {
- return a > b ? a : b;
- }
- int main()
- {
- unsigned int N, k;
- cin >> N >> k;
- vector<unsigned int> south;
- vector<unsigned int> north;
- long long int sum = 0;
- int input;
- for (unsigned int i = 0; i < N; i++)
- {
- cin >> input;
- sum += input;
- if (input > 0)
- {
- north.push_back(input);
- } else {
- south.push_back(abs(input));
- }
- };
- sort(north.begin(), north.end());
- sort(south.begin(), south.end());
- unsigned long long int sum_south = 0, sum_north = 0;
- for (unsigned int i = south.size() - 1; ; i--)
- {
- sum_south += south[i];
- if (i == 0 || i == south.size() - k) break;
- };
- for (unsigned int i = north.size() - 1; ; i--)
- {
- sum_north += north[i];
- if (i == 0 || i == north.size() - k) break;
- };
- cout<< maxi (sum - 2*sum_north, sum + 2*sum_south);
- return 0;
- }
- // GCC reference:
- // https://gcc.gnu.org/
- // C++ language references:
- // https://msdn.microsoft.com/library/3bstk3k5.aspx
- // http://www.cplusplus.com/
- // https://isocpp.org/
- // http://www.open-std.org/jtc1/sc22/wg21/
- // Boost libraries references:
- // http://www.boost.org/doc/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement