/
BinaryString.java
76 lines (69 loc) · 1.16 KB
/
BinaryString.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package infosys;
import java.util.Scanner;
public class BinaryString {
static String str;
static int cash, n, a, b;
static void swapf() {
char s[] = str.toCharArray();
int i = 0;
for (int a = 0; a < s.length; a++)
if (s[a] == '1') {
i = a;
break;
}
int j = s.length - 1;
while (j > i) {
if (cash < a)
break;
if (s[j] == '0') {
if (s[i] == '0')
i++;
else {
char temp = s[i];
s[i] = s[j];
s[j] = temp;
cash -= a;
j--;
}
} else
j--;
}
str = new String(s);
}
static void flipf() {
char s[] = str.toCharArray();
int i = 0;
for (int a = 0; a < s.length; a++)
if (s[a] == '1') {
i = a;
break;
}
while (cash >= b) {
if (i == s.length)
break;
if (s[i] == '1') {
s[i] = '0';
i++;
cash -= b;
}
}
str = new String(s);
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
str = sc.next();
cash = sc.nextInt();
a = sc.nextInt();
b = sc.nextInt();
if (a < b) {
swapf();
flipf();
} else {
flipf();
swapf();
}
System.out.println(Integer.parseInt(str, 2));
sc.close();
}
}