title: Longest subsequence in array
body: <p>I am trying to solve my task using a List and I know I am very close to solving it but I am stuck now. Something is not ok in the code and I can not figure out what it is. Could you please take a look and help:</p>
  
  <pre><code>        /*
  Write a program that reads an array of integers and removes from it a minimal number of elements in such way that the
  remaining array is sorted in increasing order. Print the remaining sorted array.
  Example: {6, 1, 4, 3, 0, 3, 6, 4, 5}  {1, 3, 3, 4, 5}
   */
  using System;
  using System.Collections.Generic;
  
  class RemoveMinimalElements
  {
  
      static void Main()
      {
          int n;
          n = int.Parse(Console.ReadLine());
          List&lt;int&gt; arr = new List&lt;int&gt;();
          List&lt;int&gt; sorted = new List&lt;int&gt;();
          int maxSubsetLenght = 0;
  
          for (int i = 0; i &lt; n; i++)
          {
              arr.Add(int.Parse(Console.ReadLine()));
          }
  
  
          for (int i = 1; i &lt;= (int)Math.Pow(2, n) - 1; i++)
          {
              int tempSubsetLenght = 0;
              string tempString = "";
              List&lt;int&gt; temp = new List&lt;int&gt;();
  
  
              for (int j = 1; j &lt;= n; j++)
              {
                  int andMask = i &amp; (1 &lt;&lt; j);
                  int bit = andMask &gt;&gt; j;
  
                  if (bit == 1)
                  {
                      temp.Add(arr[n - 1 - j]);
                      tempSubsetLenght++;
                  }
                  if (tempSubsetLenght &gt; maxSubsetLenght)
                  {
                      maxSubsetLenght = tempSubsetLenght;
  
                      for(int k =1; k &lt; temp.Count; k ++)
                      {
                          if (temp[k] &gt;= temp[k - 1])
                          {
                              sorted = temp;
                          }
                      }
                  }
              }
          }
  
          for (int i = sorted.Count - 1; i &gt; 0; i--)
          {
              Console.WriteLine(sorted[i]);
          }
      }
  }
  </code></pre>