<p>Given an integer <code>n</code>, return <code>true</code> <em>if it is possible to represent </em><code>n</code><em> as the sum of distinct powers of three.</em> Otherwise, return <code>false</code>.</p>

<p>An integer <code>y</code> is a power of three if there exists an integer <code>x</code> such that <code>y == 3<sup>x</sup></code>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> n = 12
<strong>Output:</strong> true
<strong>Explanation:</strong> 12 = 3<sup>1</sup> + 3<sup>2</sup>
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> n = 91
<strong>Output:</strong> true
<strong>Explanation:</strong> 91 = 3<sup>0</sup> + 3<sup>2</sup> + 3<sup>4</sup>
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> n = 21
<strong>Output:</strong> false
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= n &lt;= 10<sup>7</sup></code></li>
</ul>

class Solution:
    def checkPowersOfThree(self, n: int) -> bool:
        @cache
        def dfs(cur, i):
            if cur == n:
                return True
            if i == len(choices):
                return False            
            if cur + choices[i] > n:
                return False
            if dfs(cur + choices[i], i + 1) or dfs(cur, i + 1):
                return True            
            return False       

        choices = []
        x = 1
        while x <= n:
            choices.append(x)
            x *= 3
        return dfs(0, 0)

        # @cache
        # def dfs(cur, candidates):
        #     if cur == 0:
        #         return True
        #     if len(candidates) == 0:
        #         return False            
        #     for i, x in enumerate(candidates):
        #         if cur >= x:
        #             if dfs(cur - x, candidates[:i] + candidates[i+1:]):
        #                 return True
        #         else:
        #             break
        #     return False
        
443