Program for activity selection exceeds time limit I've been trying to solve this problem for some while and the solution I have come up with exceeds the lime limit by 1-3 ms and 2 out of 10 tests get a memory issue error. Why is that? This is the code for my solution:

#include &lt;fstream&gt;
#include &lt;vector&gt;
#include &lt;algorithm&gt;
#include &lt;iostream&gt;

using namespace std;

ifstream in("planificare.in");
ofstream out("planificare.out");

struct show {
    int start, finish, used = 0;
};

bool end_time (show lhs, show rhs) {
    if (lhs.finish == rhs.finish) return lhs.start &lt; rhs.start;
    return lhs.finish &lt; rhs.finish;
}

int main() {
    int participants, tv_chan;
    vector&lt;show&gt; prog;
    in &gt;&gt; participants &gt;&gt; tv_chan;
    for (int i = 1; i &lt;= participants; i++) {
        show a;
        in &gt;&gt; a.start &gt;&gt; a.finish;
        prog.push_back(a);
    }
    sort(prog.begin(), prog.end(), end_time);
    int maxActivities = 0;
    int unused = 0;
    for (int i = 1; i &lt;= tv_chan; i++) {
        while (prog[unused].used)
            unused++;
        int lastFinish = prog[unused].finish;
        prog[unused].used = 1;
        maxActivities ++;
        for (int j = 0; j &lt; prog.size(); j++) {
            if (prog[j].start &gt;= lastFinish &amp;&amp; prog[j].used == 0) {
                maxActivities++;
                lastFinish = prog[j].finish;
                prog[j].used = 1;
            }
        }
    }
    out &lt;&lt; maxActivities;
}


Planificare - InfoArena


  Talent Day is coming soon at the Blomkvist TV channel and the CEO Mike
  needs you to make the program grid. \$N\$ participants have enrolled
  to expose their talents, each communicating the amount of time it
  needs. Mike's TV Chain consists of \$K\$ stations, (Blomkvist 1, Blomkvist 2,
  ... Blomkvist K) that transmit independently of each other. Due to the
  fact that all \$K\$ stations are all just as popular as the other, the participants are indifferent
  to which they will appear. 
  
  Knowing that at any given moment any station
  will broadcast a single show, determining the maximum number of shows
  that can be televised. 
  
  Input data
  
  The input file planificare.in will contain on the first line \$2\$ natural numbers: \$N\$
  and \$K\$. On each of the following \$N\$ lines there will be 2 values,
  the starting time and the end time, representing the time interval during which the
  participant performs its activity. 
  
  Output data
  
  The output file planificare.out will contain the number requested by Mike on
  the first line. 
  
  Restrictions and clarifications
  
  \$1 \le N \le 100,000\$
  
  \$1 \le K \le 100,000\$
  
  \$1 \le \text{start}_i \le \text{stop}_i \le 1,000,000,000\$
  
  Time limit: 0.2 seconds
  
  For 30% of the tests, \$N\$ ≤ 2000 and for another 10% of the tests, \$K\$ = 1.
     At each channel, a show can start at the same time that the previous one is over.
  
  Example
  
  planificare.in

2 1
1 4
4 8

  
  planificare.out

2
The problem is that you are erasing elements from prog and then without bounds checking access it with prog[j]. That is sure to fall over.

Your end_time function is commonly known as operator&lt; So you should use the appropriate name.

There is no comment on the significance of 1 and why a program should not be reordered when it starts at 1. It seems that different programs with starting time 1 should still have an ordering.

You are inconsistent in your loops. Once goes from 1 to &lt;= participants the other goes from 0 to &lt; participants. The latter is the commonly used one.

The first loop seems to sort for start time? You can achieve the same if you create a proper operator&lt;.

using namespace std; is bad practice. There is no benefit to it other that you do not really know what is standard and what not, which is actually bad. Also it isprone to name clashes. Use what you need and not everything C++ has in the box