// goodwincommaderek@gmail.com import java.util.ArrayDeque; import java.util.ArrayList; import java.util.HashMap; import java.util.Scanner; public class optimizedcheating_dg_ac { // hi it's the devil here // telling you to cheat on your exams >:3 // do it, it will make my evil powers increase in strength >:3 // so that i can gain enough power to take over the world and outlaw your cats from meowing >:3 // (just kidding, all it will do is kick you out of school. don't do it) // and with that, we return to your regularly-scheduled judge solution public static void main(String[] args) { Scanner in = new Scanner(System.in); int N = in.nextInt(); int M = in.nextInt(); int K = in.nextInt() - 1; long[] memory = new long[N]; for (int n = 0; n < N; n++) { memory[n] = in.nextLong(); } ArrayList plusItBy = new ArrayList<>(); ArrayList minusItBy = new ArrayList<>(); ArrayList timesItBy = new ArrayList<>(); ArrayList overItBy = new ArrayList<>(); ArrayList whichOp_p = new ArrayList<>(); ArrayList whichOp_m = new ArrayList<>(); ArrayList whichOp_t = new ArrayList<>(); ArrayList whichOp_o = new ArrayList<>(); for (int m = 0; m < M; m++) { String op = in.next(); ArrayList whichList = null; ArrayList whichOp = null; switch (op.charAt(0)) { case '+': whichList = plusItBy; whichOp = whichOp_p; break; case '-': whichList = minusItBy; whichOp = whichOp_m; break; case '*': whichList = timesItBy; whichOp = whichOp_t; break; case '/': whichList = overItBy; whichOp = whichOp_o; break; default: System.out.println("your data is bad and you should feel bad"); } whichList.add(in.nextLong()); whichOp.add(m + 1); } // first check whether the value is already unique long startValue = memory[K]; boolean isUnique = true; for (int n = 0; n < N; n++) { if (n == K) continue; if (startValue == memory[n]) { isUnique = false; break; } } if (isUnique) { System.out.println(0); } else { // fine ok. let's BFS ArrayDeque bfs = new ArrayDeque<>(); bfs.addLast(memory[K]); HashMap prevs = new HashMap<>(); HashMap opOfPrevs = new HashMap<>(); for (int n = 0; n < N; n++) { prevs.put(memory[n], -1L); } prevs.put(memory[K], -2L); long endOfPath = -1; while (!bfs.isEmpty()) { Long location = bfs.pollFirst(); int i; i = 0; for (Long plus : plusItBy) { long next = location + plus; Long mapping = prevs.get(next); if (mapping == null) { endOfPath = next; prevs.put(next, location); opOfPrevs.put(next, whichOp_p.get(i)); } else if (mapping.longValue() == -1) { // no other node already put it on the queue. go here prevs.put(next, location); opOfPrevs.put(next, whichOp_p.get(i)); bfs.addLast(next); } i++; } if (endOfPath != -1) break; // this is a lot of copypasted code. hopefully i don't mess up any of it i = 0; for (Long minus : minusItBy) { long next = location - minus; if (next < 0) { i++; continue; } Long mapping = prevs.get(next); if (mapping == null) { endOfPath = next; prevs.put(next, location); opOfPrevs.put(next, whichOp_m.get(i)); } else if (mapping.longValue() == -1) { // no other node already put it on the queue. go here prevs.put(next, location); opOfPrevs.put(next, whichOp_m.get(i)); bfs.addLast(next); } i++; } if (endOfPath != -1) break; i = 0; for (Long times : timesItBy) { long next = location * times; Long mapping = prevs.get(next); if (mapping == null) { endOfPath = next; prevs.put(next, location); opOfPrevs.put(next, whichOp_t.get(i)); } else if (mapping.longValue() == -1) { // no other node already put it on the queue. go here prevs.put(next, location); opOfPrevs.put(next, whichOp_t.get(i)); bfs.addLast(next); } i++; } if (endOfPath != -1) break; i = 0; for (Long over : overItBy) { long next = location / over; Long mapping = prevs.get(next); if (mapping == null) { endOfPath = next; prevs.put(next, location); opOfPrevs.put(next, whichOp_o.get(i)); } else if (mapping.longValue() == -1) { // no other node already put it on the queue. go here prevs.put(next, location); opOfPrevs.put(next, whichOp_o.get(i)); bfs.addLast(next); } i++; } if (endOfPath != -1) break; } if (endOfPath == -1) { System.out.println(-1); } else { ArrayList opsReverseOrder = new ArrayList<>(); while (endOfPath != -2) { opsReverseOrder.add(opOfPrevs.get(endOfPath)); endOfPath = prevs.get(endOfPath); } StringBuilder sb = new StringBuilder(); sb.append(opsReverseOrder.size()-1).append('\n'); for (int i = opsReverseOrder.size() - 2; i >= 0; i--) { sb.append(opsReverseOrder.get(i)).append('\n'); } System.out.print(sb); } } } }