View Code of Problem 33

import java.util.*;
class room {
	int power;
	String mode;
	boolean iflast = false;
}
public class Main {
	public static void main(String[] args){
		try{
			Scanner in = new Scanner(System.in);
			int t = in.nextInt();
			for(int i =0;i<t;i++) {
				int m = in.nextInt();
				ArrayList<ArrayList<Integer>> migong = new ArrayList<ArrayList<Integer>>();
				for(int j =0;j<m;j++){
					ArrayList<Integer> part = new ArrayList<Integer>();
					int n =in.nextInt();
					for(int l =0;l<n;l++){
						part.add(in.nextInt());
					}
					migong.add(part);
				}
				for(ArrayList<Integer> al :migong){
					al.add(0, in.nextInt());
				}
				int road = 0;
				for(ArrayList<Integer> al :migong){
					road+=al.size();
				}
				room[] r = new room[road];
				int count = 0;
				for(ArrayList<Integer> al :migong){
					for(int j=0;j<al.size();j++) {
						room oner =new room();
						if(j==0) {
							oner.mode = "food";					
						}else {
							oner.mode = "iceheart";	
						}
						oner.power = al.get(j);
						if(count==road-1) {
							oner.iflast = true;
						}
						r[count++]=oner;
					}
				}
				System.out.println(attack(0,0,r));
			}
		}catch(Exception ex) {		
		}
	}
	public static int attack(int start,int local,room[] r) {
		if(r[local].iflast) {
			if(start>=r[local].power) {
				return 1;
			}else {
				return 0;
			}
		}
		if(r[local].mode.equals("food")) {
			return attack(start+r[local].power,local+1,r);
		}else {
			int notgetit = attack(start,local+1,r);
			if(start>=r[local].power) {
				int getit = 1+attack(start-r[local].power,local+1,r);
				if(getit>notgetit) {
					return getit;
				}else {
					return notgetit;
				}
			}
			return notgetit;
		}
	}
}

Double click to view unformatted code.


Back to problem 33