I am implementing matrix chain multiplication program in c++. I am using ONLINE_JUDGE flag to write output to the file. When i run the program it does not produce correct output in file, but it is producing correct output in console. 
My Program:
#include<bits/stdc++.h>
using namespace std;
int main(){
    #ifndef ONLINE_JUDGE 
        // For getting input from input.txt file 
        freopen("C:\\Users\\Rahul kumar\\desktop\\Algorithm\\input.txt", "r", stdin); 
        // Printing the Output to output.txt file 
        freopen("C:\\Users\\Rahul kumar\\desktop\\Algorithm\\output.txt", "w", stdout); 
    #endif 
    // dimension of four matrices
    vector<pair<int,int>>matrices;
    matrices.push_back(make_pair(5,4));
    matrices.push_back(make_pair(4,6));
    matrices.push_back(make_pair(6,2));
    matrices.push_back(make_pair(2,7));
    vector<int>p; // algorithm helper data
    int last; // column of last matrix. 
    for(auto matrix:matrices){
        p.push_back(matrix.first);
        last=matrix.second;
    }    
    p.push_back(last);  // add last matrix. 
    int numberOfMatrix=matrices.size();
    int dp[numberOfMatrix][numberOfMatrix];
    for(int i=0;i<numberOfMatrix;i++){
        for(int j=0;j<numberOfMatrix;j++){
            dp[i][j]=0;
        }
    }
    for(int i=0;i<numberOfMatrix;i++){
        for(int j=0;j<numberOfMatrix;j++){
            if(i==j){
                dp[i][j]=0;
            }else if(j==i+1){
                dp[i][j]=p[i]*p[i+1]*p[i+2];
            }else{
                int best=5000;
                for(int k=i;k<j;k++){
                    printf("dp[%d][%d]=%d, dp[%d][%d]=%d, p[%d]=%d, p[%d]=%d, p[%d]=%d\n",i,k,dp[i][k],k+1,j,dp[k+1][j],i,p[i],k+1,p[k+1],j+1,p[j+1]);
                    best=min(best,dp[i][k]+dp[k+1][j]+p[i]*p[k+1]*p[j+1]);
                    cout<<"I: "<<i<<" J:  "<<j<<" Best: "<<best<<endl;
                }
                dp[i][j]=best;
            }
        }
    }
    for(int i=0;i<numberOfMatrix;i++){
        for(int j=0;j<numberOfMatrix;j++){
            cout<<dp[i][j]<<"   ";
        }
        cout<<endl;
    }
    return 0;
}
OUTPUT in output.txt file:
I: 0 J:  2 Best: 40
I: 0 J:  2 Best: 40
I: 0 J:  3 Best: 140
I: 0 J:  3 Best: 140
I: 0 J:  3 Best: 110
I: 1 J:  3 Best: 168
I: 1 J:  3 Best: 104
0   120   40   110   
5000   0   48   104   
5000   5000   0   84   
5000   5000   5000   0   
dp[0][0]=0, dp[1][2]=0, p[0]=5, p[1]=4, p[3]=2
dp[0][1]=120, dp[2][2]=0, p[0]=5, p[2]=6, p[3]=2
dp[0][0]=0, dp[1][3]=0, p[0]=5, p[1]=4, p[4]=7
dp[0][1]=120, dp[2][3]=0, p[0]=5, p[2]=6, p[4]=7
dp[0][2]=40, dp[3][3]=0, p[0]=5, p[3]=2, p[4]=7
dp[1][1]=0, dp[2][3]=0, p[1]=4, p[2]=6, p[4]=7
dp[1][2]=48, dp[3][3]=0, p[1]=4, p[3]=2, p[4]=7
Not expected output. 
 When i comment this code
#ifndef ONLINE_JUDGE 
        // For getting input from input.txt file 
        freopen("C:\\Users\\Rahul kumar\\desktop\\Algorithm\\input.txt", "r", stdin); 
        // Printing the Output to output.txt file 
        freopen("C:\\Users\\Rahul kumar\\desktop\\Algorithm\\output.txt", "w", stdout); 
    #endif 
and run the program then the output on the console is..
dp[0][0]=0, dp[1][2]=0, p[0]=5, p[1]=4, p[3]=2
I: 0 J:  2 Best: 40
dp[0][1]=120, dp[2][2]=0, p[0]=5, p[2]=6, p[3]=2
I: 0 J:  2 Best: 40
dp[0][0]=0, dp[1][3]=0, p[0]=5, p[1]=4, p[4]=7
I: 0 J:  3 Best: 140
dp[0][1]=120, dp[2][3]=0, p[0]=5, p[2]=6, p[4]=7
I: 0 J:  3 Best: 140
dp[0][2]=40, dp[3][3]=0, p[0]=5, p[3]=2, p[4]=7
I: 0 J:  3 Best: 110
dp[1][1]=0, dp[2][3]=0, p[1]=4, p[2]=6, p[4]=7
I: 1 J:  3 Best: 168
dp[1][2]=48, dp[3][3]=0, p[1]=4, p[3]=2, p[4]=7
I: 1 J:  3 Best: 104
0   120   40   110
5000   0   48   104
5000   5000   0   84
5000   5000   5000   0
I: 1 J:  3 Best: 104
0   120   40   110
5000   0   48   104
5000   5000   0   84
5000   5000   5000   0
This output is expected, but different form output of the file. This happens many times with me. I am running this program on vs code.
 
    