Language: C++

#include <bits/stdc++.h>
 
using namespace std;
typedef long long ll;
typedef long double ld;

template <class T>
using lim = numeric_limits<T>;

struct cake{
	ll x, y, z;
	
	cake(){x=y=z=0;}
	cake(int a, int b, int c){
		x = a;
		y = b;
		z = c;
	}
	
	cake operator+(const cake &other)const{
		cake c;
		c.x = x + other.x;
		c.y = y + other.y;
		c.z = z + other.z;
		return c;
	}
	
	ll val(){
		return abs(x)+abs(y)+abs(z);
	}
};

bool compare1(const cake &a, const cake &b){return a.x+a.y+a.z < b.x+b.y+b.z;}
bool compare2(const cake &a, const cake &b){return a.x+a.y-a.z < b.x+b.y-b.z;}
bool compare3(const cake &a, const cake &b){return a.x-a.y+a.z < b.x-b.y+b.z;}
bool compare4(const cake &a, const cake &b){return a.x-a.y-a.z < b.x-b.y-b.z;}
bool compare5(const cake &a, const cake &b){return -a.x+a.y+a.z < -b.x+b.y+b.z;}
bool compare6(const cake &a, const cake &b){return -a.x+a.y-a.z < -b.x+b.y-b.z;}
bool compare7(const cake &a, const cake &b){return -a.x-a.y+a.z < -b.x-b.y+b.z;}
bool compare8(const cake &a, const cake &b){return -a.x-a.y-a.z < -b.x-b.y-b.z;}

ll solve(cake arr[], int m){
	cake ans;
	for (int i = 0; i < m; i++){
		ans = ans+arr[i];
	}
	return ans.val();
}

int main() {
	ios_base::sync_with_stdio(false) ;
	cin.tie(0);
	
	int n, m;
	cin >> n >> m;
	
	cake arr[n];
	
	for (int i = 0; i < n; i++){
		cin >> arr[i].x >> arr[i].y >> arr[i].z;
	}
	
	ll ans = 0;
	sort(ans, ans+n, compare1);
	ans = max(ans,solve(arr,m));
	sort(ans, ans+n, compare2);
	ans = max(ans,solve(arr,m));
	sort(ans, ans+n, compare3);
	ans = max(ans,solve(arr,m));
	sort(ans, ans+n, compare4);
	ans = max(ans,solve(arr,m));
	sort(ans, ans+n, compare5);
	ans = max(ans,solve(arr,m));
	sort(ans, ans+n, compare6);
	ans = max(ans,solve(arr,m));
	sort(ans, ans+n, compare7);
	ans = max(ans,solve(arr,m));
	sort(ans, ans+n, compare8);
	ans = max(ans,solve(arr,m));
	
	cout << ans << endl;
}



Compiler output:
In file included from /usr/include/c++/14/algorithm:61,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:51,
                 from a.cc:1:
/usr/include/c++/14/bits/stl_algo.h: In instantiation of 'void std::__insertion_sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = long long int; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<bool (*)(const cake&, const cake&)>]':
/usr/include/c++/14/bits/stl_algo.h:1817:25:   required from 'void std::__final_insertion_sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = long long int; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<bool (*)(const cake&, const cake&)>]'
 1817 |           std::__insertion_sort(__first, __first + int(_S_threshold), __comp);
      |           ~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algo.h:1908:31:   required from 'void std::__sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = long long int; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<bool (*)(const cake&, const cake&)>]'
 1908 |           std::__final_insertion_sort(__first, __last, __comp);
      |           ~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algo.h:4805:18:   required from 'void std::sort(_RAIter, _RAIter, _Compare) [with _RAIter = long long int; _Compare = bool (*)(const cake&, const cake&)]'
 4805 |       std::__sort(__first, __last, __gnu_cxx::__ops::__iter_comp_iter(__comp));
      |       ~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
a.cc:64:6:   required from here
   64 |         sort(ans, ans+n, compare1);
      |         ~~~~^~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algo.h:1780:17: error: no type named 'value_type' in 'struct std::iterator_traits<long long int>'
 1780 |                 __val = _GLIBCXX_MOVE(*__i);
      |                 ^~~~~
In file included from /usr/include/c++/14/bits/stl_pair.h:61,
                 from /usr/include/c++/14/bits/stl_algobase.h:64,
                 from /usr/include/c++/14/algorithm:60:
/usr/include/c++/14/bits/stl_algo.h:1780:25: error: invalid type argument of unary '*' (have 'long long int')
 1780 |                 __val = _GLIBCXX_MOVE(*__i);
      |                         ^~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algo.h:1782:15: error: invalid type argument of unary '*' (have 'long long int')
 1782 |               *__first = _GLIBCXX_MOVE(__val);
      |               ^~~~~~~~
In file included from /usr/include/c++/14/bits/stl_algobase.h:71:
/usr/include/c++/14/bits/predefined_ops.h: In instantiation of 'constexpr bool __gnu_cxx::__ops::_Iter_comp_iter<_Compare>::operator()(_Iterator1, _Iterator2) [with _Iterator1 = long long int; _Iterator2 = long long int; _Compare = bool (*)(const cake&, const cake&)]':
/usr/include/c++/14/bits/stl_algo.h:1777:14:   required from 'void std::__insertion_sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = long long int; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<bool (*)(const cake&, const cake&)>]'
 1777 |           if (__comp(__i, __first))
      |               ~~~~~~^~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algo.h:1817:25:   required from 'void std::__final_insertion_sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = long long int; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<bool (*)(const cake&, const cake&)>]'
 1817 |           std::__insertion_sort(__first, __first + int(_S_threshold), __comp);
      |           ~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algo.h:1908:31:   required from 'void std::__sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = long long int; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<bool (*)(const cake&, const cake&)>]'
 1908 |           std::__final_insertion_sort(__first, __last, __comp);
      |           ~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algo.h:4805:18:   required from 'void std::sort(_RAIter, _RAIter, _Compare) [with _RAIter = long long int; _Compare = bool (*)(const cake&, const cake&)]'
 4805 |       std::__sort(__first, __last, __gnu_cxx::__ops::__iter_comp_iter(__comp));
      |       ~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
a.cc:64:6:   required from here
   64 |         sort(ans, ans+n, compare1);
      |         ~~~~^~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/predefined_ops.h:158:31: error: invalid type argument of unary '*' (have 'long long int')
  158 |         { return bool(_M_comp(*__it1, *__it2)); }
      |                               ^~~~~~
/usr/include/c++/14/bits/predefined_ops.h:158:39: error: invalid type argument of unary '*' (have 'long long int')
  158 |         { return bool(_M_comp(*__it1, *__it2)); }
      |                                       ^~~~~~
In file included from /usr/include/c++/14/bits/stl_algo.h:61:
/usr/include/c++/14/bits/stl_heap.h: In instantiation of 'void std::__make_heap(_RandomAccessIterator, _RandomAccessIterator, _Compare&) [with _RandomAccessIterator = long long int; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<bool (*)(const cake&, const cake&)>]':
/usr/include/c++/14/bits/stl_algo.h:1593:23:   required from 'void std::__heap_select(_RandomAccessIterator, _RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = long long int; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<bool (*)(const cake&, const cake&)>]'
 1593 |       std::__make_heap(__first, __middle, __comp);
      |       ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algo.h:1868:25:   required from 'void std::__partial_sort(_RandomAccessIterator, _RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = long long int; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<bool (*)(const cake&, const cake&)>]'
 1868 |       std::__heap_select(__first, __middle, __last, __comp);
      |       ~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algo.h:1884:27:   required from 'void std::__introsort_loop(_RandomAccessIterator, _RandomAccessIterator, _Size, _Compare) [with _RandomAccessIterator = long long int; _Size = long long int; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<bool (*)(const cake&, const cake&)>]'
 1884 |               std::__partial_sort(__first, __last, __last, __comp);
      |               ~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algo.h:1905:25:   required from 'void std::__sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = long long int; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<bool (*)(const cake&, const cake&)>]'
 1905 |           std::__introsort_loop(__first, __last,
      |           ~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~
 1906 |                                 std::__lg(__last - __first) * 2,
      |                                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 1907 |                                 __comp);
      |                                 ~~~~~~~
/usr/include/c++/14/bits/stl_algo.h:4805:18:   required from 'void std::sort(_RAIter, _RAIter, _Compare) [with _RAIter = long long int; _Compare = bool (*)(const cake&, const cake&)]'
 4805 |       std::__sort(__first, __last, __gnu_cxx::__ops::__iter_comp_iter(__comp));
      |       ~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
a.cc:64:6:   required from here
   64 |         sort(ans, ans+n, compare1);
      |         ~~~~^~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_heap.h:344:11: error: no type named 'value_type' in 'struct std::iterator_traits<long long int>'
  344 |           _ValueType;
      |           ^~~~~~~~~~
/usr/include/c++/14/bits/stl_heap.h:346:11: error: no type named 'difference_type' in 'struct std::iterator_traits<long long int>'
  346 |           _DistanceType;
      |           ^~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_heap.h: In instantiation of 'void std::__pop_heap(_RandomAccessIterator, _RandomAccessIterator, _RandomAccessIterator, _Compare&) [with _RandomAccessIterator = long long int; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<bool (*)(const cake&, const cake&)>]':
/usr/include/c++/14/bits/stl_algo.h:1596:19:   required from 'void std::__heap_select(_RandomAccessIterator, _RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = long long int; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<bool (*)(const cake&, const cake&)>]'
 1596 |           std::__pop_heap(__first, __middle, __i, __comp);
      |           ~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algo.h:1868:25:   required from 'void std::__partial_sort(_RandomAccessIterator, _RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = long long int; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<bool (*)(const cake&, const cake&)>]'
 1868 |       std::__heap_select(__first, __middle, __last, __comp);
      |       ~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algo.h:1884:27:   required from 'void std::__introsort_loop(_RandomAccessIterator, _RandomAccessIterator, _Size, _Compare) [with _RandomAccessIterator = long long int; _Size = long long int; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<bool (*)(const cake&, const cake&)>]'
 1884 |               std::__partial_sort(__first, __last, __last, __comp);
      |               ~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algo.h:1905:25:   required from 'void std::__sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = long long int; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<bool (*)(const cake&, const cake&)>]'
 1905 |           std::__introsort_loop(__first, __last,
      |           ~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~
 1906 |                                 std::__lg(__last - __first) * 2,
      |                                 ~~~~~~