Language: C++

#include <iostream>
#include <cstdio>
#include <vector>
#include <deque>
#include <algorithm>
#include <climits>
 
using namespace std;
 
typedef long long ll;
typedef unsigned long long ull;
#define REP(i, n) for(int i=0; i<n; i++)
 
int main(){
    int H,W;
    cin >> H >> W;
    if(H%3 == 0 || W%3 == 0){
        cout << 0 << endl;
    }else{
        ull ans = LLONG_MAX;
        ull h = H, w = W;
        for(ull i = 1; i < w; i++){
            ull S1 = i*h;
            ull S2, S3;
            ull diff;
            if(h % 2 == 1){
                S2 = (w-i)*(h-1)/2;
                S3 = (w-i)*(h+1)/2;
                ull MAX = max(S1, max(S2, S3));
                ull MIN = min(S1, min(S2, S3));
                diff = MAX - MIN;
            }
            else{
                S2 = (w-i)*(h/2);
                //printf("S2:%lld\tw-i:%d\th/2:%d\n", S2, w-i, h/2);
                diff = abs(S1 - S2);
            }
            ans = min(ans, diff);
            //printf("ans:%d\tdiff:%lld\tS1:%lld\tS2:%lld\n", ans, diff, S1, S2);
        }
        for(ull i = 1; i < h; i++){
            ull S1 = i*w;
            ull S2, S3;
            ull diff;
            if(h % 2 == 1){
                S2 = (h-i)*(w-1)/2;
                S3 = (h-i)*(w+1)/2;
                ull MAX = max(S1, max(S2, S3));
                ull MIN = min(S1, min(S2, S3));
                diff = MAX - MIN;
            }
            else{
                S2 = (h-i)*w/2;
                diff = abs(S1 - S2);
            }
            ans = min(ans, diff);
        }
            cout << ans << endl;
    }
    return 0;
}


Compiler output:
a.cc: In function 'int main()':
a.cc:36:27: error: call of overloaded 'abs(ull)' is ambiguous
   36 |                 diff = abs(S1 - S2);
      |                        ~~~^~~~~~~~~
In file included from /usr/include/c++/14/cstdlib:79,
                 from /usr/include/c++/14/ext/string_conversions.h:43,
                 from /usr/include/c++/14/bits/basic_string.h:4154,
                 from /usr/include/c++/14/string:54,
                 from /usr/include/c++/14/bits/locale_classes.h:40,
                 from /usr/include/c++/14/bits/ios_base.h:41,
                 from /usr/include/c++/14/ios:44,
                 from /usr/include/c++/14/ostream:40,
                 from /usr/include/c++/14/iostream:41,
                 from a.cc:1:
/usr/include/stdlib.h:980:12: note: candidate: 'int abs(int)'
  980 | extern int abs (int __x) __THROW __attribute__ ((__const__)) __wur;
      |            ^~~
In file included from /usr/include/c++/14/cstdlib:81:
/usr/include/c++/14/bits/std_abs.h:137:3: note: candidate: 'constexpr __float128 std::abs(__float128)'
  137 |   abs(__float128 __x)
      |   ^~~
/usr/include/c++/14/bits/std_abs.h:85:3: note: candidate: 'constexpr __int128 std::abs(__int128)'
   85 |   abs(__GLIBCXX_TYPE_INT_N_0 __x) { return __x >= 0 ? __x : -__x; }
      |   ^~~
/usr/include/c++/14/bits/std_abs.h:79:3: note: candidate: 'constexpr long double std::abs(long double)'
   79 |   abs(long double __x)
      |   ^~~
/usr/include/c++/14/bits/std_abs.h:75:3: note: candidate: 'constexpr float std::abs(float)'
   75 |   abs(float __x)
      |   ^~~
/usr/include/c++/14/bits/std_abs.h:71:3: note: candidate: 'constexpr double std::abs(double)'
   71 |   abs(double __x)
      |   ^~~
/usr/include/c++/14/bits/std_abs.h:61:3: note: candidate: 'long long int std::abs(long long int)'
   61 |   abs(long long __x) { return __builtin_llabs (__x); }
      |   ^~~
/usr/include/c++/14/bits/std_abs.h:56:3: note: candidate: 'long int std::abs(long int)'
   56 |   abs(long __i) { return __builtin_labs(__i); }
      |   ^~~
a.cc:54:27: error: call of overloaded 'abs(ull)' is ambiguous
   54 |                 diff = abs(S1 - S2);
      |                        ~~~^~~~~~~~~
/usr/include/stdlib.h:980:12: note: candidate: 'int abs(int)'
  980 | extern int abs (int __x) __THROW __attribute__ ((__const__)) __wur;
      |            ^~~
/usr/include/c++/14/bits/std_abs.h:137:3: note: candidate: 'constexpr __float128 std::abs(__float128)'
  137 |   abs(__float128 __x)
      |   ^~~
/usr/include/c++/14/bits/std_abs.h:85:3: note: candidate: 'constexpr __int128 std::abs(__int128)'
   85 |   abs(__GLIBCXX_TYPE_INT_N_0 __x) { return __x >= 0 ? __x : -__x; }
      |   ^~~
/usr/include/c++/14/bits/std_abs.h:79:3: note: candidate: 'constexpr long double std::abs(long double)'
   79 |   abs(long double __x)
      |   ^~~
/usr/include/c++/14/bits/std_abs.h:75:3: note: candidate: 'constexpr float std::abs(float)'
   75 |   abs(float __x)
      |   ^~~
/usr/include/c++/14/bits/std_abs.h:71:3: note: candidate: 'constexpr double std::abs(double)'
   71 |   abs(double __x)
      |   ^~~
/usr/include/c++/14/bits/std_abs.h:61:3: note: candidate: 'long long int std::abs(long long int)'
   61 |   abs(long long __x) { return __builtin_llabs (__x); }
      |   ^~~
/usr/include/c++/14/bits/std_abs.h:56:3: note: candidate: 'long int std::abs(long int)'
   56 |   abs(long __i) { return __builtin_labs(__i); }
      |   ^~~