using
System;
using
System.Collections.Generic;
class
GFG {
class
area {
public
int
a, b;
public
area(
int
a,
int
b)
{
this
.a = a;
this
.b = b;
}
};
class
Pair {
public
int
first, second;
public
Pair(
int
first,
int
second)
{
this
.first = first;
this
.second = second;
}
}
static
int
max(
int
a,
int
b,
int
c)
{
return
Math.Max(a, Math.Max(b, c));
}
static
int
maxSurvival(
int
A,
int
B, area X, area Y,
area Z,
int
last,
Dictionary<Pair,
int
> memo)
{
if
(A <= 0 || B <= 0)
return
0;
Pair cur =
new
Pair(A, B);
if
(memo.ContainsKey(cur))
return
memo[cur];
int
temp = 0;
switch
(last) {
case
1:
temp
= 1
+ Math.Max(maxSurvival(A + Y.a, B + Y.b,
X, Y, Z, 2, memo),
maxSurvival(A + Z.a, B + Z.b,
X, Y, Z, 3, memo));
break
;
case
2:
temp
= 1
+ Math.Max(maxSurvival(A + X.a, B + X.b,
X, Y, Z, 1, memo),
maxSurvival(A + Z.a, B + Z.b,
X, Y, Z, 3, memo));
break
;
case
3:
temp
= 1
+ Math.Max(maxSurvival(A + X.a, B + X.b,
X, Y, Z, 1, memo),
maxSurvival(A + Y.a, B + Y.b,
X, Y, Z, 2, memo));
break
;
}
memo[cur] = temp;
return
temp;
}
static
int
getMaxSurvivalTime(
int
A,
int
B, area X,
area Y, area Z)
{
if
(A <= 0 || B <= 0)
return
0;
Dictionary<Pair,
int
> memo
=
new
Dictionary<Pair,
int
>();
return
max(
maxSurvival(A + X.a, B + X.b, X, Y, Z, 1, memo),
maxSurvival(A + Y.a, B + Y.b, X, Y, Z, 2, memo),
maxSurvival(A + Z.a, B + Z.b, X, Y, Z, 3,
memo));
}
public
static
void
Main(String[] args)
{
area X =
new
area(3, 2);
area Y =
new
area(-5, -10);
area Z =
new
area(-20, 5);
int
A = 20;
int
B = 8;
Console.WriteLine(
getMaxSurvivalTime(A, B, X, Y, Z));
}
}