August 2021

D - Lakshmi Aunty's Food Stall

Solution

\[\]

Implementation using C++

#include <cstdio>

using namespace std;

int main() {
    int t, n, m, i, j;
    scanf("%d", &t);
    
    int a[100][100];
    int dp[100][101];
    
    int m1, m2, pos, ans;
    
    while(t--) {
        scanf("%d%d", &n, &m);
        
        for (i = 0; i < n; i++)
            for (j = 0; j < m; j++)
                scanf("%d", &a[i][j]);
      	
        /*
            First row of dp is initialised to corresponding
            elements of first row of a. The last element of
            dp[0] is initalised to 0.
        */
        
        for (i = 0; i < m; i++)
            dp[0][i] = a[0][i];
        
        dp[0][m] = 0;
        
        /*
            For all the rows, i from 1 to n-1, find the largest
            and second largest element in dp[i-1] and keep note
            of the position of the largest element.
            
            Initialize all the elements of dp in the ith row
            according to the follwing
            
                dp[i][j] = a[i][j]+max(dp[i-1])             when j != pos
                dp[i][pos] = a[i][j]+secondmax(dp[i-1])	    when j == pos
                dp[i][M] = max(dp[i-1])
        */
        
        for (i = 1; i < n; i++) {
            if (dp[i-1][0] > dp[i-1][1]) {
                m1 = dp[i-1][0];
                m2 = dp[i-1][1];
                
                pos = 0;
            }
            else {
                m1 = dp[i-1][1];
                m2 = dp[i-1][0];
                
                pos = 1;
            }
            
            for (j = 2; j < m; j++) {
                if (dp[i-1][j] > m1) {
                    m2 = m1;
                    m1 = dp[i-1][j];
                    
                    pos = j;
                }
                else if (dp[i-1][j] > m2)
                    m2 = dp[i-1][j];
            }
            
            for (j = 0; j < m; j++) {
                if (j == pos)
                    dp[i][j] = m2+a[i][j];
                else
                    dp[i][j] = m1+a[i][j];
            }
            
            dp[i][m] = m1;
        }
        
        /*
            The maximum possible revenue is the
            largest element in the last row of dp
        */
        
        ans = dp[n-1][0];
        
        for (i = 1; i <= m; i++)
            if (dp[n-1][i] > ans)
                ans = dp[n-1][i];
        
        printf("%d\n", ans);
    }
    
    return 0;
}
\[\]

Implementation using Java

import java.io.*;
import java.util.*;

public class Solution {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        
        int t, n, m, i, j;
        
        int a[][] = new int[100][100];
        int dp[][] = new int[100][101];
        
        int m1, m2, pos, ans;
        
        t = scan.nextInt();
        scan.nextLine();
        
        while (t-- > 0) {
            n = scan.nextInt();
            m = scan.nextInt();
            scan.nextLine();

            for (i = 0; i < n; i++) {
                for (j = 0; j < m; j++)
                    a[i][j] = scan.nextInt();
                
                scan.nextLine();
            }

            /*
                First row of dp is initialised to corresponding
                elements of first row of a. The last element of
                dp[0] is initalised to 0.
            */

            for (i = 0; i < m; i++)
                dp[0][i] = a[0][i];

            dp[0][m] = 0;

            /*
                For all the rows, i from 1 to n-1, find the largest
                and second largest element in dp[i-1] and keep note
                of the position of the largest element.

                Initialize all the elements of dp in the ith row
                according to the follwing

                    dp[i][j] = a[i][j]+max(dp[i-1])             when j != pos
                    dp[i][pos] = a[i][j]+secondmax(dp[i-1])        when j == pos
                    dp[i][M] = max(dp[i-1])
            */

            for (i = 1; i < n; i++) {
                if (dp[i-1][0] > dp[i-1][1]) {
                    m1 = dp[i-1][0];
                    m2 = dp[i-1][1];

                    pos = 0;
                }
                else {
                    m1 = dp[i-1][1];
                    m2 = dp[i-1][0];

                    pos = 1;
                }

                for (j = 2; j < m; j++) {
                    if (dp[i-1][j] > m1) {
                        m2 = m1;
                        m1 = dp[i-1][j];

                        pos = j;
                    }
                    else if (dp[i-1][j] > m2)
                        m2 = dp[i-1][j];
                }

                for (j = 0; j < m; j++) {
                    if (j == pos)
                        dp[i][j] = m2+a[i][j];
                    else
                        dp[i][j] = m1+a[i][j];
                }

                dp[i][m] = m1;
            }

            /*
                The maximum possible revenue is the
                largest element in the last row of dp
            */

            ans = dp[n-1][0];

            for (i = 1; i <= m; i++)
                if (dp[n-1][i] > ans)
                    ans = dp[n-1][i];

            System.out.println(ans);
        }
    }
}
\[\]

Implementation using Python

for _ in range(int(input())):
    n, m = map(int, input().split())
    
    a = []
    for i in range(0, n):
        a.append(list(map(int, input().split())))
        
    dp = []
    for i in range(0, n):
        temp = []
        for j in range(0, m+1):
            temp.append(0)
            
        dp.append(temp)
        
    # First row of dp is initialied with elements of first row of a
    for i in range(0, m):
        dp[0][i] = a[0][i]
        
    # Last element of dp[0] is 0
    dp[0][m] = 0
        
    '''
        For all the rows, i from 1 to n-1, find the largest
        and second largest element in dp[i-1] and keep note
        of the position of the largest element.
            
        Initialize all the elements of dp in the ith row
        according to the follwing
            
            dp[i][j] = a[i][j]+max(dp[i-1])             when j != pos
            dp[i][pos] = a[i][j]+secondmax(dp[i-1])        when j == pos
            arr.append(max(dp[i-1]))
    '''
        
    for i in range(1, n):
        maxpos = 0
        
        m1 = dp[i-1][0]
        m2 = dp[i-1][1]
        
        if m2 > m1:
            m2, m1 = m1, m2
            maxpos = 1
        
        for j in range(2, m):
            if dp[i-1][j] > m1:
                m2 = m1
                m1 = dp[i-1][j]
                
                maxpos = j
            elif dp[i-1][j] > m2:
                m2 = dp[i-1][j]
                
        for j in range(0, m):
            if j == maxpos:
                dp[i][j] += m2+a[i][j]
            else:
                dp[i][j] += m1+a[i][j]
                
        dp[i][m] = m1
    
    # The maximum possible revenue is the largest element in the last row of dp
    print(max(dp[n-1]))
\[\]

Contest Material