Language: C++

#include<algorithm>
#include<math.h>
using namespace std;
typedef long long ll;

//hが奇数でwを動かすとき
int odd(int h, int w, int x,int m) {
	if (h*x >= (h + 1)*(w - x) / 2)return min(m, h*x - (h - 1)*(w - x) / 2);
	if (h*x >= (h - 1)*(w - x) / 2)return min(m, w - x);
	if (h*x < (h - 1)*(w - x) / 2)return min(m, -h*x + (h + 1)*(w - x) / 2);
}

int main(void) {
	int h, w, ans = 10000000;
	cin >> h >> w;
	if (h % 3 == 0 || w % 3 == 0)cout << 0 << endl;
	else {
		if (h % 2 == 0 && w % 2 == 0) {
			for (int i = 1;i < h;i++)ans = min(ans, abs(w*i - w / 2 * (h - i)));
			for (int i = 1;i < w;i++)ans = min(ans, abs(h*i - h / 2 * (w - i)));
			cout << ans << endl;
		}
		else {
			if (h % 2 == 0) {
				for (int i = 1;i < w;i++)ans = min(ans, abs(h*i - h / 2 * (w - i)));
				for (int i = 1;i < h;i++)ans = min(ans, odd(w, h, i, ans));
				cout << ans << endl;
			}
			else {
				if (w % 2 == 0) {
					for (int i = 1;i < h;i++)ans = min(ans, abs(w*i - w / 2 * (h - i)));
					for (int i = 1;i < w;i++)ans = min(ans, odd(h, w, i, ans));
					cout << ans << endl;
				}
				else {
					for (int i = 1;i < h;i++)ans = min(ans, odd(w, h, i, ans));
					for (int i = 1;i < w;i++)ans = min(ans, odd(h, w, i, ans));
					cout << ans << endl;
				}
			}
		}
	}
	return 0;
}

Compiler output:
a.cc: In function 'int main()':
a.cc:15:9: error: 'cin' was not declared in this scope
   15 |         cin >> h >> w;
      |         ^~~
a.cc:3:1: note: 'std::cin' is defined in header '<iostream>'; this is probably fixable by adding '#include <iostream>'
    2 | #include<math.h>
  +++ |+#include <iostream>
    3 | using namespace std;
a.cc:16:38: error: 'cout' was not declared in this scope
   16 |         if (h % 3 == 0 || w % 3 == 0)cout << 0 << endl;
      |                                      ^~~~
a.cc:16:38: note: 'std::cout' is defined in header '<iostream>'; this is probably fixable by adding '#include <iostream>'
a.cc:16:51: error: 'endl' was not declared in this scope
   16 |         if (h % 3 == 0 || w % 3 == 0)cout << 0 << endl;
      |                                                   ^~~~
a.cc:3:1: note: 'std::endl' is defined in header '<ostream>'; this is probably fixable by adding '#include <ostream>'
    2 | #include<math.h>
  +++ |+#include <ostream>
    3 | using namespace std;
a.cc:21:25: error: 'cout' was not declared in this scope
   21 |                         cout << ans << endl;
      |                         ^~~~
a.cc:21:25: note: 'std::cout' is defined in header '<iostream>'; this is probably fixable by adding '#include <iostream>'
a.cc:21:40: error: 'endl' was not declared in this scope
   21 |                         cout << ans << endl;
      |                                        ^~~~
a.cc:21:40: note: 'std::endl' is defined in header '<ostream>'; this is probably fixable by adding '#include <ostream>'
a.cc:27:33: error: 'cout' was not declared in this scope
   27 |                                 cout << ans << endl;
      |                                 ^~~~
a.cc:27:33: note: 'std::cout' is defined in header '<iostream>'; this is probably fixable by adding '#include <iostream>'
a.cc:27:48: error: 'endl' was not declared in this scope
   27 |                                 cout << ans << endl;
      |                                                ^~~~
a.cc:27:48: note: 'std::endl' is defined in header '<ostream>'; this is probably fixable by adding '#include <ostream>'
a.cc:33:41: error: 'cout' was not declared in this scope
   33 |                                         cout << ans << endl;
      |                                         ^~~~
a.cc:33:41: note: 'std::cout' is defined in header '<iostream>'; this is probably fixable by adding '#include <iostream>'
a.cc:33:56: error: 'endl' was not declared in this scope
   33 |                                         cout << ans << endl;
      |                                                        ^~~~
a.cc:33:56: note: 'std::endl' is defined in header '<ostream>'; this is probably fixable by adding '#include <ostream>'
a.cc:38:41: error: 'cout' was not declared in this scope
   38 |                                         cout << ans << endl;
      |                                         ^~~~
a.cc:38:41: note: 'std::cout' is defined in header '<iostream>'; this is probably fixable by adding '#include <iostream>'
a.cc:38:56: error: 'endl' was not declared in this scope
   38 |                                         cout << ans << endl;
      |                                                        ^~~~
a.cc:38:56: note: 'std::endl' is defined in header '<ostream>'; this is probably fixable by adding '#include <ostream>'
a.cc: In function 'int odd(int, int, int, int)':
a.cc:11:1: warning: control reaches end of non-void function [-Wreturn-type]
   11 | }
      | ^