title: What is wrong with this solution while solving JUICE on SPOJ?
body: <p>The problem is:</p>
  
  <p>Jerry loses himself in the interesting game: Fruit Ninja. Fruit Ninja is a game of iPhone and iPad in which the players cut the fruits coming from the bottom of the screen and gain the bonus from cutting more than two fruits with a single slice. Once a fruit is cut, it breaks into small pieces and cannot be cut any more.</p>
  
  <p>After months of training, he becomes pro of this game. Actually, he can cut all the fruits on the screen at any time. Jerry also has a bad habit that he has no willing to leave some fruits for the future cutting. In the other words, after Jerry cuts the fruits, all the fruits on the screen breaks and no one left. That is why all his friends call him Juice Extractor.</p>
  
  <p>Now he only consider about the bonus, when he cuts more than two fruits, he can gain some bonus scores as same as the number of fruits he slice at that time. For example, if Jerry cuts 4 fruits with a single slice, he can get 4 scores from this slice.</p>
  
  <p>After Jerry gets the fruit schedule, he knows the appearing time and the disappearing time for every single fruit. He can only cut a fruit into pieces between its appearing time and disappearing time inclusive. He wants to know the maximum possible bonus scores he can receive.</p>
  
  <p>Input</p>
  
  <p>There are several test cases; the first line of the input contains a single integer T, denoting the number of the test cases. (T &lt;= 200)</p>
  
  <p>For each test case, the first line contains an integer N, denoting the total number of fruits. (1 &lt;= N &lt;= 1000)</p>
  
  <p>The next N lines, each line describe a fruit. For each line, there are two integers Xi and Yi, where Xi is the appearing time of the fruit and Yi is the disappearing time of this fruit. (0 &lt;= Xi &lt;= Yi &lt;= 1000000000)</p>
  
  <p>Output</p>
  
  <p>For each test case, output a single integer denoting the maximum scores that Jerry could possibly gain. See the sample for further details.</p>
  
  <p>Example</p>
  
  <p>Input:
  1
  10
  1 10
  2 11
  3 12
  4 13
  13 14
  14 15
  13 19
  20 22
  21 23
  22 24</p>
  
  <p>Output:
  Case #1: 10</p>
  
  <p>Here is the code :
  It sorts the time intervals on basis of start time.dp[i] accounts for the time when ith fruit appears. We will be making a cut only when a fruit appears, as it will cover all the cases</p>
  
  <pre><code>#include&lt;iostream&gt;
  #include&lt;vector&gt;
  #include&lt;algorithm&gt;
  #include&lt;memory.h&gt;
  using namespace std;
  struct node
  {
      int x,y;
  };
  int cmp(node a,node b)
  {
      if(a.x!=b.x)
      return a.x&lt;b.x;
      return a.y&lt;b.y;
  }
  int main()
  {
      int test;
      cin&gt;&gt;test;
      int t=0;
      int dp[1002];
      while(test--)
      {
          t++;
          int n;
          cin&gt;&gt;n;
          vector&lt;node&gt; v(n);
          for(int i=0;i&lt;n;i++)
          {
              int a,b;
              cin&gt;&gt;a&gt;&gt;b;
              v[i].x=a;
              v[i].y=b;
          }
          sort(v.begin(),v.end(),cmp);
          memset(dp,0,sizeof(dp));
          int mx=0;
          for(int i=0;i&lt;n;i++)
          {
              int id=100000,match=0;
              for(int j=0;j&lt;=i;j++)
              {
                  if(v[j].y&gt;=v[i].x)
                  {
                      id=min(id,j);
                      match++;
                  }
              }
              for(int j=id;j&lt;=i;j++)
              {
                  if(match&gt;2)
                  dp[i+1]=max(dp[i+1],dp[j]+match);
                  else
                  dp[i+1]=max(dp[i+1],dp[j]);
                  if(v[j].y&gt;=v[i].x)
                  match--;
              }
  
              dp[i+1]=max(dp[i+1],dp[i]);
              mx=max(mx,dp[i+1]);
  
          }
          cout&lt;&lt;"Case #"&lt;&lt;t&lt;&lt;": "&lt;&lt;mx&lt;&lt;"\n";
      }
      return 0;
  }
  </code></pre>