using
System;
class
Solution {
public
void
Rp(
string
s,
int
x,
int
y,
ref
long
ans,
bool
flag)
{
string
str =
""
;
foreach
(
char
c
in
s)
{
if
(c ==
'p'
) {
if
(!
string
.IsNullOrEmpty(str)
&& str[str.Length - 1] ==
'r'
) {
ans += y;
str = str.Remove(str.Length - 1);
}
else
{
str += c;
}
}
else
{
str += c;
}
}
if
(flag) {
Pr(str, x, y,
ref
ans,
false
);
}
}
public
void
Pr(
string
s,
int
x,
int
y,
ref
long
ans,
bool
flag)
{
string
str =
""
;
foreach
(
char
c
in
s)
{
if
(c ==
'r'
) {
if
(!
string
.IsNullOrEmpty(str)
&& str[str.Length - 1] ==
'p'
) {
ans += x;
str = str.Remove(str.Length - 1);
}
else
{
str += c;
}
}
else
{
str += c;
}
}
if
(flag) {
Rp(str, x, y,
ref
ans,
false
);
}
}
public
long
Solve(
int
x,
int
y,
string
s)
{
long
ans = 0;
if
(x > y) {
Pr(s, x, y,
ref
ans,
true
);
}
else
{
Rp(s, x, y,
ref
ans,
true
);
}
return
ans;
}
public
static
void
Main()
{
string
s =
"abppprrr"
;
int
x = 5, y = 4;
Solution obj =
new
Solution();
long
answer = obj.Solve(x, y, s);
Console.WriteLine(
"Maximize cost obtained by removal of substrings 'pr' or 'rp': "
+ answer);
}
}