Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- import java.io.*;
- import java.io.BufferedReader;
- import java.awt.Point;
- public class movie{
- public static final String TASKNAME = "movie";
- public static long mod= 1000000007;
- public static Debug db;
- public static void main(String[] args) throws IOException {
- // InputReader in = new InputReader(System.in);
- // PrintWriter out = new PrintWriter(System.out);
- InputReader in = new InputReader(new FileInputStream(TASKNAME+".in"));
- PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(TASKNAME+".out")));
- Autocompletion solver = new Autocompletion();
- db=new Debug(System.getSecurityManager()==null);
- solver.solve(1, in, out);
- out.close();
- }
- static class Autocompletion {
- public void solve(int testNumber, InputReader in, PrintWriter out) {
- int n=in.nextInt();
- int l=in.nextInt();
- int dur[]=new int[n];
- ArrayList<Pair> arr=new ArrayList<Pair>();//index by order
- for(int i=0;i<n;i++) {
- int d=in.nextInt();
- dur[i]=d;
- int c=in.nextInt();
- for(int j=0;j<c;j++) {
- int temp=in.nextInt();
- arr.add(new Pair(temp,i));
- }
- }
- Collections.sort(arr);
- ArrayList<Integer> adj[]=new ArrayList[arr.size()];
- for(int i=0;i<arr.size();i++)adj[i]=new ArrayList<Integer>();
- for(int i=0;i<arr.size();i++) {
- for(int j=i+1;j<arr.size();j++) {
- if(arr.get(i).x+dur[arr.get(i).y]>=arr.get(j).x) {
- adj[i].add(j);
- }
- else {
- break;
- }
- }
- }
- int dp[]=new int[1<<n];
- Arrays.fill(dp, -1);
- dp[0]=0;
- ArrayDeque<Pair> dq=new ArrayDeque<Pair>();
- for(int i=0;i<arr.size();i++) {
- if(arr.get(i).x==0) {
- dq.add(new Pair(i,1<<arr.get(i).y));
- dp[1<<arr.get(i).y]=dur[arr.get(i).y];
- }
- else {
- break;
- }
- }
- while(!dq.isEmpty()) {
- Pair x=dq.remove();
- for(int y:adj[x.x]) {
- int newSet=(1<<arr.get(y).y | x.y);
- if((1<<arr.get(y).y & x.y)==0) {//no movies in common
- if(arr.get(y).x+dur[arr.get(y).y]>dp[newSet]) {//better
- dp[newSet]=arr.get(y).x+dur[arr.get(y).y];
- dq.add(new Pair(y,newSet));
- }
- }
- }
- }
- int ret=22;
- for(int i=0;i<dp.length;i++) {
- if(dp[i]>=l) {
- // System.out.println(i);
- ret=Math.min(ret, Integer.bitCount(i));
- }
- }
- if(ret==22) {
- out.println(-1);
- }
- else {
- out.println(ret);
- }
- }
- }
- static class Pair implements Comparable<Pair>{
- int x;
- int y;
- Pair(int a, int b){
- x=a;
- y=b;
- }
- @Override
- public int compareTo(Pair arg0) {
- if(arg0.x!=x)return x-arg0.x;
- return y-arg0.y;
- }
- }
- static class Triple implements Comparable<Triple>{
- int x;
- int y;
- int z;
- Triple(int a, int b, int c){
- x=a;
- y=b;
- z=c;
- }
- @Override
- public int compareTo(Triple arg0) {
- if(arg0.x!=x)return x-arg0.x;
- return y-arg0.y;
- }
- }
- static class InputReader {
- public BufferedReader reader;
- public StringTokenizer tokenizer;
- public InputReader(InputStream stream) {
- reader = new BufferedReader(new InputStreamReader(stream), 32768);
- tokenizer = null;
- }
- public String next() {
- while (tokenizer == null || !tokenizer.hasMoreTokens()) {
- try {
- tokenizer = new StringTokenizer(reader.readLine());
- } catch (IOException e) {
- throw new RuntimeException(e);
- }
- }
- return tokenizer.nextToken();
- }
- public int nextInt() {
- return Integer.parseInt(next());
- }
- public long nextLong() {
- return Long.parseLong(next());
- }
- public int[] nextIntArr(int n) {
- int arr[]=new int[n];
- for(int i=0;i<n;i++) {
- arr[i]=this.nextInt();
- }
- return arr;
- }
- }
- public static class Debug {
- private boolean allowDebug;
- public Debug(boolean allowDebug) {
- this.allowDebug = allowDebug;
- }
- private void outputName(String name) {
- System.out.print(name + " = ");
- }
- public void debug(String name, int x) {
- if (!allowDebug) {
- return;
- }
- outputName(name);
- System.out.println("" + x);
- }
- public void debug(String name, long x) {
- if (!allowDebug) {
- return;
- }
- outputName(name);
- System.out.println("" + x);
- }
- public void debug(String name, double x) {
- if (!allowDebug) {
- return;
- }
- outputName(name);
- System.out.println("" + x);
- }
- public void debug(String name, int[] x) {
- if (!allowDebug) {
- return;
- }
- outputName(name);
- System.out.println(Arrays.toString(x));
- }
- public void debug(String name, long[] x) {
- if (!allowDebug) {
- return;
- }
- outputName(name);
- System.out.println(Arrays.toString(x));
- }
- public void debug(String name, double[] x) {
- if (!allowDebug) {
- return;
- }
- outputName(name);
- System.out.println(Arrays.toString(x));
- }
- public void debug(String name, Object x) {
- if (!allowDebug) {
- return;
- }
- outputName(name);
- System.out.println("" + x);
- }
- public void debug(String name, Object... x) {
- if (!allowDebug) {
- return;
- }
- outputName(name);
- System.out.println(Arrays.deepToString(x));
- }
- }
- }
- //equal :problem
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement