#P5083. Two-variable Function Maximization
Two-variable Function Maximization
Two-variable Function Maximization
We are given a function \(f(a,b)\) defined as follows:
If \(a<2\) or \(b<2\), then \(f(a,b)=a+b\).
Otherwise, let \[ \begin{aligned} v_1 &= a+b,\\ v_2 &= g(\lfloor a/2 \rfloor)+g(\lfloor a/3 \rfloor)+g(\lfloor a/8 \rfloor)+g(\lfloor a/9 \rfloor)+b,\\ v_3 &= g(\lfloor b/2 \rfloor)+g(\lfloor b/3 \rfloor)+g(\lfloor b/8 \rfloor)+g(\lfloor b/9 \rfloor)+a,\\ v_4 &= g(\lfloor a/2 \rfloor)+g(\lfloor a/3 \rfloor)+g(\lfloor a/8 \rfloor)+g(\lfloor a/9 \rfloor)+g(\lfloor b/2 \rfloor)+g(\lfloor b/3 \rfloor)+g(\lfloor b/8 \rfloor)+g(\lfloor b/9 \rfloor), \end{aligned} \] and define \[ f(a,b)=\max(v_1,\;v_2,\;v_3,\;v_4). \]
The helper function \(g(n)\) is defined as:
\[ g(n)= \begin{cases} 0, & n=0,\\ \max\Bigl(g(\lfloor n/2 \rfloor)+g(\lfloor n/3 \rfloor)+g(\lfloor n/8 \rfloor)+g(\lfloor n/9 \rfloor),\; n\Bigr), & n>0. \end{cases} \]
Your task is to calculate \(f(a,b)\) for given non-negative integers \(a\) and \(b\), where \(a,b < 10^{16}\). Due to potential overlapping subproblems, memoization is required to compute \(g(n)\) efficiently.
inputFormat
The input consists of a single line containing two space-separated non-negative integers \(a\) and \(b\) (with \(a,b < 10^{16}\)).
outputFormat
Output a single integer: the value of \(f(a,b)\).
sample
1 1
2