Language: C++

#include <algorithm>
#include <iostream>
#include <unordered_map>
#include <vector>

struct Context {
	std::vector<int>& blocks;
	std::unordered_map<uint64_t, int> cache;
};

struct State {
	uint64_t data;

	State() : data(0) {}
	State(uint64_t data) : data(data) {}

	State mark(int index) {
		return data | (((uint64_t)1) << index);
	}

	bool is_marked(int index) {
		return (data & (((uint64_t)1) << index)) != 0;
	}
};

int search(Context& ctx, State state, int count) {
	if (ctx.blocks.size() == 0) return count;
	auto& cache_iter = ctx.cache.find(state.data);
	if (cache_iter != ctx.cache.end()) return (*cache_iter).second;

	int max = count;

	for (int i = 0; i < ctx.blocks.size() - 1; i++) {
		if (state.is_marked(i)) continue;

		// ?¬????????????????????????¢???
		for (int j = i + 1; j < ctx.blocks.size(); j++) {
			if (state.is_marked(j)) continue;
			if (std::abs(ctx.blocks[i] - ctx.blocks[j]) > 1) break;

			int result = search(ctx, state.mark(i).mark(j), count + 2);
			if (result > max) max = result;
		}
	}

	ctx.cache[state.data] = max;
	return max;
}

int main()
{
	std::vector<int> blocks;
	blocks.reserve(300);

	while (true) {
		int num_blocks;
		std::cin >> num_blocks;

		if (num_blocks == 0) return 0;

		blocks.clear();
		for (int i = 0; i < num_blocks; i++)
		{
			int w;
			std::cin >> w;
			blocks.push_back(w);
		}

		Context ctx{ blocks, {} };
		std::cout << search(ctx, {}, 0) << std::endl;
	}
}

Compiler output:
a.cc:8:28: error: 'uint64_t' was not declared in this scope
    8 |         std::unordered_map<uint64_t, int> cache;
      |                            ^~~~~~~~
a.cc:5:1: note: 'uint64_t' is defined in header '<cstdint>'; this is probably fixable by adding '#include <cstdint>'
    4 | #include <vector>
  +++ |+#include <cstdint>
    5 | 
a.cc:8:41: error: template argument 1 is invalid
    8 |         std::unordered_map<uint64_t, int> cache;
      |                                         ^
a.cc:8:41: error: template argument 3 is invalid
a.cc:8:41: error: template argument 4 is invalid
a.cc:8:41: error: template argument 5 is invalid
a.cc:12:9: error: 'uint64_t' does not name a type
   12 |         uint64_t data;
      |         ^~~~~~~~
a.cc:12:9: note: 'uint64_t' is defined in header '<cstdint>'; this is probably fixable by adding '#include <cstdint>'
a.cc:15:23: error: expected ')' before 'data'
   15 |         State(uint64_t data) : data(data) {}
      |              ~        ^~~~~
      |                       )
a.cc: In constructor 'State::State()':
a.cc:14:19: error: class 'State' does not have any field named 'data'
   14 |         State() : data(0) {}
      |                   ^~~~
a.cc: In member function 'State State::mark(int)':
a.cc:18:24: error: 'data' was not declared in this scope; did you mean 'std::data'?
   18 |                 return data | (((uint64_t)1) << index);
      |                        ^~~~
      |                        std::data
In file included from /usr/include/c++/14/string:53,
                 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:2:
/usr/include/c++/14/bits/range_access.h:344:5: note: 'std::data' declared here
  344 |     data(initializer_list<_Tp> __il) noexcept
      |     ^~~~
a.cc:18:34: error: 'uint64_t' was not declared in this scope
   18 |                 return data | (((uint64_t)1) << index);
      |                                  ^~~~~~~~
a.cc:18:34: note: 'uint64_t' is defined in header '<cstdint>'; this is probably fixable by adding '#include <cstdint>'
a.cc:18:43: error: expected ')' before numeric constant
   18 |                 return data | (((uint64_t)1) << index);
      |                                           ^
a.cc:18:32: note: to match this '('
   18 |                 return data | (((uint64_t)1) << index);
      |                                ^
a.cc:18:55: error: expected ')' before ';' token
   18 |                 return data | (((uint64_t)1) << index);
      |                                                       ^
a.cc:18:31: note: to match this '('
   18 |                 return data | (((uint64_t)1) << index);
      |                               ^
a.cc: In member function 'bool State::is_marked(int)':
a.cc:22:25: error: 'data' was not declared in this scope; did you mean 'std::data'?
   22 |                 return (data & (((uint64_t)1) << index)) != 0;
      |                         ^~~~
      |                         std::data
/usr/include/c++/14/bits/range_access.h:344:5: note: 'std::data' declared here
  344 |     data(initializer_list<_Tp> __il) noexcept
      |     ^~~~
a.cc:22:35: error: 'uint64_t' was not declared in this scope
   22 |                 return (data & (((uint64_t)1) << index)) != 0;
      |                                   ^~~~~~~~
a.cc:22:35: note: 'uint64_t' is defined in header '<cstdint>'; this is probably fixable by adding '#include <cstdint>'
a.cc:22:44: error: expected ')' before numeric constant
   22 |                 return (data & (((uint64_t)1) << index)) != 0;
      |                                            ^
a.cc:22:33: note: to match this '('
   22 |                 return (data & (((uint64_t)1) << index)) != 0;
      |                                 ^
a.cc:22:62: error: expected ')' before ';' token
   22 |                 return (data & (((uint64_t)1) << index)) != 0;
      |                                                              ^
a.cc:22:32: note: to match this '('
   22 |                 return (data & (((uint64_t)1) << index)) != 0;
      |                                ^
a.cc:22:62: error: expected ')' before ';' token
   22 |                 return (data & (((uint64_t)1) << index)) != 0;
      |                                                              ^
a.cc:22:24: note: to match this '('
   22 |                 return (data & (((uint64_t)1) << index)) != 0;
      |                        ^
a.cc: In function 'int search(Context&, State, int)':
a.cc:28:38: error: request for member 'find' in 'ctx.Context::cache', which is of non-class type 'int'
   28 |         auto& cache_iter = ctx.cache.find(state.data);
      |                                      ^~~~
a.cc:28:49: error: 'struct State' has no member named 'data'
   28 |         auto& cache_iter = ctx.cache.find(state.data);
      |                                                 ^~~~
a.cc:29:37: error: request for member 'end' in 'ctx.Context::cache', which is of non-class type 'int'
   29 |         if (cache_iter != ctx.cache.end()) return (*cache_iter).second;
      |                                     ^~~
a.cc:46:25: error: 'struct State' has no member named 'data'
   46 |         ctx.cache[state.data] = max;
      |                         ^~~~