title: K Th Largest Sum Contiguous Subarray
signature: def kThLargestSumContiguousSubarray(arr: List[Int], n: Int, k: Int) = {
source_py: def k_th_largest_sum_contiguous_subarray(arr, n, k):
      sum = []
      sum.append(0)
      sum.append(arr[0])
      for i in range(2, (n + 1)):
          sum.append((sum[(i - 1)] + arr[(i - 1)]))
      Q = []
      heapq.heapify(Q)
      for i in range(1, (n + 1)):
          for j in range(i, (n + 1)):
              x = (sum[j] - sum[(i - 1)])
              if (len(Q) < k):
                  heapq.heappush(Q, x)
              elif (Q[0] < x):
                  heapq.heappop(Q)
                  heapq.heappush(Q, x)
      return Q[0]
source_cpp: int k_th_largest_sum_contiguous_subarray(vector<int> arr, int n, int k) {
    int sum [ n + 1 ];
    sum [ 0 ] = 0;
    sum [ 1 ] = arr [ 0 ];
    for ( int i = 2;
    i <= n;
    i ++ ) sum [ i ] = sum [ i - 1 ] + arr [ i - 1 ];
    priority_queue < int, vector < int >, greater < int > > Q;
    for ( int i = 1;
    i <= n;
    i ++ ) {
      for ( int j = i;
      j <= n;
      j ++ ) {
        int x = sum [ j ] - sum [ i - 1 ];
        if ( Q . size ( ) < k ) Q . push ( x );
        else {
          if ( Q . top ( ) < x ) {
            Q . pop ( );
            Q . push ( x );
          }
        }
      }
    }
    return Q . top ( );
  }