title: Getting wrong answer for Arranging Tasks #1704 in urionlinejudge
body: <p>Your boss has told you that you have only one computer to complete some tasks. Each of them earns the company an amount v of money and must be finished up to t hours from now. After that, it cannot be performed anymore and has no more value. The computer completes exactly one task per hour.</p>
  
  <p>You want to impress your boss so he might give you a promotion. To do that, you intend to use your programming skills to select which tasks execute in such a way that the amount of money lost is minimized.</p>
  
  <p>Input</p>
  
  <p>The input is composed of several test cases and ends with end of file. Each one describes a list of tasks and starts with two integers N (1 ≤N) and H (H ≤ 1000), which are, respectively, the number of tasks and the number of hours that the computer is available. Then follow N lines, each one with two integer v (1 ≤ v ≤ 1000) and t (1 ≤ t ≤ H) described above.</p>
  
  <p>Output</p>
  
  <p>For each test case output a line with an integer representing the minimum lost money.</p>
  
  <pre><code>Sample Input    Sample Output
  3 3             0   
  5 1             3
  10 2
  20 3
  4 2
  1 2 
  2 1 
  4 1 
  2 2
  </code></pre>
  
  <p>I am solving with many complex test cases like given below, getting expected results but still getting 100% wrong answer on submission. My approach is sorting the array in descending order according to the money-value and then below logic.What is wrong in my algorithm?</p>
  
  <pre><code>Input       Output
  5 3         30
  10 1        30 
  20 2        1
  50 2        0
  40 3         
  30 3
  4 5    test case 1
  10 1
  20 1
  50 2
  40 2 
  6 5    test case 2
  10 1
  12 4 
  15 5
  20 5
  1 5
  30 5
  3 1000
  1000 1000
  2 1
  3 3
  </code></pre>
  
  <p>In the below code - test case 1(4 5) and test case 2(6 5) works fine as tlarge is less for test case 1 and most tasks in test case 2 can be done in 5 hrs so the equal flag has not been set to true.</p>
  
  <pre><code>while(hours &lt; H &amp;&amp; z &lt; N)
              {
                  if(equal[Arr[z].getT()]!=true &amp;&amp; hours&lt;tlarge) {
                          loss -= Arr[z].getV();
                          equal[Arr[z].getT()]=true;
                          hours++;
                      }
                  else
                  {
                      int time=Arr[z].getT()-1;
                      while(time&gt;0)
                      {
                          if(equal[time]==false)
                          {
                              loss -= Arr[z].getV();
                              hours++;
                              if(Arr[z].getT()&lt;=hours)
                              {
                                  int time1 = Arr[z].getT()-1;
                                  while(time1&gt;0)
                                  {
                                      equal[time1]=true;
                                      time1--;
                                  }
                              }
                              break;
                          }
                          time--;
                      }
                  }
                  z++;
              }
  </code></pre>
  
  <p>Here is my code -</p>
  
  <pre class="lang-cs prettyprint-override"><code>class Entry
  {
      private int v;
      private int t;
  
      Entry(int v, int t) {
            this.v = v;
            this.t = t;
      }
  
      public int getV() {
          return v;
      }
  
      public int getT() {
          return t;
      }  
  }
  
  public class Main {
      public final static Entry[] Arr = new Entry[1003];
      public static void main (String[] args) throws java.lang.Exception
      {
          BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
  
          while(true){
                  String T = in.readLine();
                  if(T==null) {
                      break;
                  }
                  String[] spl = T.split(" ");
                  int N = Integer.parseInt(spl[0]);
                  int H = Integer.parseInt(spl[1]);
  
                  int Num = N;int tlarge = 0;
                  int hours = 0;int loss = 0;int z=0,y=0;
                  boolean equal[] = new boolean[1003];
  
                  while( Num &gt; 0)
                  {
                      String oper = in.readLine();
                      String[] op = oper.split(" ");
                      int v = Integer.parseInt(op[0]);
                      int t = Integer.parseInt(op[1]);
                      Arr[Num-1] = new Entry(v,t);
                      Num--;
  
                  }
                  while(y&lt;N)
                  {
                      loss += Arr[y].getV();
                      if(Arr[y].getT()&gt;tlarge) tlarge=Arr[y].getT();
                      equal[Arr[y].getT()]=false;
                      y++;
                  }
                  QuickSort( 0, N-1);//from zero to no of tasks
  
                  while(hours &lt; H &amp;&amp; z &lt; N)
                  {
                      if((equal[Arr[z].getT()]!=true) || hours&lt;Arr[z].getT())  {
                          if(Arr[z].getT()&lt;=tlarge) {
                              if(z&lt;tlarge) {
                              loss -= Arr[z].getV();
                              equal[Arr[z].getT()]=true;
                              hours++;
                              }
                          }
  
                      }
                      z++;
                  }
                  System.out.printf("%d\n",loss);
          }
      }   
  }
  </code></pre>