-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathAccountBook_UVa11832.java
154 lines (134 loc) · 3.3 KB
/
AccountBook_UVa11832.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package v118;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;
public class AccountBook_UVa11832 {
static int[][] dp;
static long[][] canPlus, canMinus;
static int N, F, OFFSET, T[];
static int solve()
{
dp = new int[N + 1][OFFSET<<1|1];
canPlus = new long[N + 1][OFFSET<<1|1];
canMinus = new long[N + 1][OFFSET<<1|1];
dp[N][F + OFFSET] = 1;
for(int idx = N - 1; idx >= 0; --idx)
{
int x = T[idx];
for(int sum = -OFFSET; sum <= OFFSET; ++sum)
{
int ret = 0;
long p = 0, m = 0;
if(sum - x >= -OFFSET)
{
int y = dp[idx + 1][sum - x + OFFSET];
if(y == 1)
{
ret = 1;
p |= canPlus[idx + 1][sum - x + OFFSET];
m |= canMinus[idx + 1][sum - x + OFFSET] | 1l<<idx;
}
}
if(sum + x <= OFFSET)
{
int y = dp[idx + 1][sum + x + OFFSET];
if(y == 1)
{
ret = 1;
p |= canPlus[idx + 1][sum + x + OFFSET] | 1l<<idx;
m |= canMinus[idx + 1][sum + x + OFFSET];
}
}
canPlus[idx][sum + OFFSET] = p;
canMinus[idx][sum + OFFSET] = m;
dp[idx][sum + OFFSET] = ret;
}
}
return dp[0][OFFSET];
}
public static void main(String[] args) throws IOException
{
Scanner sc = new Scanner(System.in);
PrintWriter out = new PrintWriter(System.out);
while(true)
{
N = sc.nextInt(); F = sc.nextInt();
if(N == 0)
break;
OFFSET = 1000 * N;
T = new int[N];
for(int i = 0; i < N; ++i)
T[i] = sc.nextInt();
if(Math.abs(F) > OFFSET || solve() != 1)
out.println("*");
else
{
StringBuilder sb = new StringBuilder();
long x = canPlus[0][OFFSET], y = canMinus[0][OFFSET];
for(int i = 0; i < N; ++i)
if((x & 1l<<i) == (y & 1l<<i))
sb.append("?");
else if((x & 1l<<i) != 0)
sb.append("+");
else
sb.append("-");
out.println(sb);
}
}
out.flush();
out.close();
}
static class Scanner
{
StringTokenizer st;
BufferedReader br;
public Scanner(InputStream s){ br = new BufferedReader(new InputStreamReader(s));}
public String next() throws IOException
{
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
public int nextInt() throws IOException {return Integer.parseInt(next());}
public long nextLong() throws IOException {return Long.parseLong(next());}
public String nextLine() throws IOException {return br.readLine();}
public double nextDouble() throws IOException
{
String x = next();
StringBuilder sb = new StringBuilder("0");
double res = 0, f = 1;
boolean dec = false, neg = false;
int start = 0;
if(x.charAt(0) == '-')
{
neg = true;
start++;
}
for(int i = start; i < x.length(); i++)
if(x.charAt(i) == '.')
{
res = Long.parseLong(sb.toString());
sb = new StringBuilder("0");
dec = true;
}
else
{
sb.append(x.charAt(i));
if(dec)
f *= 10;
}
res += Long.parseLong(sb.toString()) / f;
return res * (neg?-1:1);
}
public boolean ready() throws IOException {return br.ready();}
public boolean nextEmpty() throws IOException
{
String s = br.readLine();
st = new StringTokenizer(s);
return s.isEmpty();
}
}
}