Advertisement
Guest User

wwww

a guest
Oct 18th, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.45 KB | None | 0 0
  1. // This file is a "Hello, world!" in C++ language by GCC for wandbox.
  2. #include <iostream>
  3. #include <algorithm>
  4. #include <vector>
  5.  
  6. using namespace std;
  7. long long maxi (long long int a, long long b)
  8. {
  9.     return a > b ? a : b;
  10. }
  11. int main()
  12. {
  13.     unsigned int N, k;
  14.     cin >> N >> k;
  15.     vector<unsigned int> south;
  16.     vector<unsigned int> north;
  17.    
  18.     long long int sum = 0;
  19.     int input;
  20.     for (unsigned int i = 0; i < N; i++)
  21.     {
  22.         cin >> input;
  23.         sum += input;
  24.         if (input > 0)
  25.         {
  26.             north.push_back(input);
  27.         } else {
  28.             south.push_back(abs(input));
  29.         }
  30.     };
  31.     sort(north.begin(), north.end());
  32.     sort(south.begin(), south.end());
  33.    
  34.     unsigned long long int sum_south = 0, sum_north = 0;
  35.     for (unsigned int i = south.size() - 1; ; i--)
  36.     {
  37.         sum_south += south[i];
  38.         if (i == 0 || i == south.size() - k) break;
  39.     };
  40.     for (unsigned int i = north.size() - 1; ; i--)
  41.     {
  42.         sum_north += north[i];
  43.         if (i == 0 || i == north.size() - k) break;
  44.     };
  45.     cout<< maxi (sum - 2*sum_north, sum + 2*sum_south);
  46.     return 0;
  47. }    
  48.  
  49. // GCC reference:
  50. //   https://gcc.gnu.org/
  51.  
  52. // C++ language references:
  53. //   https://msdn.microsoft.com/library/3bstk3k5.aspx
  54. //   http://www.cplusplus.com/
  55. //   https://isocpp.org/
  56. //   http://www.open-std.org/jtc1/sc22/wg21/
  57.  
  58. // Boost libraries references:
  59. //   http://www.boost.org/doc/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement