Repository navigation
Expand file tree
/
Copy pathNetEase09051.java
More file actions
71 lines (67 loc) · 2.41 KB
/
Copy pathNetEase09051.java
File metadata and controls
71 lines (67 loc) · 2.41 KB
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
import java.util.Scanner;
import java.util.Stack;
public class NetEase0905 {
static int left = 0, right = 0, bag = 0;
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int N = in.nextInt();
int M = in.nextInt();
int[] prices = new int[N];
int[] res = new int[M];
ArrayList<Stack<Integer>> list = new ArrayList();
list.add(new Stack<Integer>());
for(int i=0; i<N; i++) {
prices[i] = in.nextInt();
Stack<Integer> stack = new Stack();
stack.push(prices[i]);
list.add(stack);
}
for(int i=0; i<M; i++) {
int k = in.nextInt();
left = 0;
right = 0;
bag = 0;
for(int j=0; j<k; j++) {
String hand = in.next();
String op = in.next();
if(hand.equals("left") && op.equals("take")) {
int num = in.nextInt();
int p = 0;
if(list.get(num).size() == 1) {
p = list.get(num).peek();
} else {
p = list.get(num).pop();
}
left = p;
} else if(hand.equals("left") && op.equals("return")) {
int num = in.nextInt();
list.get(num).push(left);
left = 0;
} else if(hand.equals("left") && op.equals("keep")) {
bag += left;
left = 0;
} else if(hand.equals("right") && op.equals("take")) {
int num = in.nextInt();
int p = 0;
if(list.get(num).size() == 1) {
p = list.get(num).peek();
} else {
p = list.get(num).pop();
}
right = p;
} else if(hand.equals("right") && op.equals("return")) {
int num = in.nextInt();
list.get(num).push(right);
right = 0;
} else if(hand.equals("right") && op.equals("keep")) {
bag += right;
right = 0;
}
}
res[i] = left + right + bag;
}
for(int i=0; i<res.length; i++) {
System.out.println(res[i]);
}
}
}