# Python Code for ROI (TCS Codevita) | PrepInsta

**ROI**

TCS CodeVita is a coding competition organized by TCS every year, in search of world’s best coder. This is a global level coding competition in which coders from all around the world compete for the title of World’s Best Coder. **ROI **is one of the sample problem of this year TCS CodeVita season 11 competition.

**Question -: **You must be aware of the concept of Stocks Portfolio. A stock portfolio is a collection of stock(s) that you invest into with an objective of making profit.

Stocks are bought and sold. Selling price minus buying price is realized profit or loss. In case a stock is not sold yet, if buying price is more than or less than the current stock market price, then it is termed as unrealized profit or loss, respectively.

Given information in form of <Quantity of Stock bought, time of purchase, time of sell, array of prices>, calculate the realized P/L and unrealized P/L at the given time.

**Constraints**

1<= No. Of Stocks (N) <=10^2

1<= Price of Stock <= 2*10^4

1<= M <= 365

1 <= Time of Purchase <= Time of Sell <= Length of list

**Input**

First line contains an integer N which denotes the number of stocks in the portfolio.

Next N lines contain a space separate tuple of 3 integers which denote < Quantity Bought, Time of Purchase, Time of Sell > for each stock. If the stock has not been sold, the Time of Sell will be 0.

The N+1 line contain an integer M which denotes number of days for which price of stock is provided

Then the next N lines contain M integers which denote the stock price from time T1 to TM.

The last line will be the time instance at which the the P/L needs to be computed.

**Output**

Print realized P/L on first line

Print unrealized P/L on the second line

**Time Limit (secs)**

1

**Examples**

**Example 1****Input**

3

10 4 20

10 1 11

100 6 0

22

113 115 112 113 115 112 113 115 112 113 115 112 113 115 112 113 115 112 113 115 112 117

52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73

101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122

5**Output**

0

60**Explanation**

From input we know the following

- Portfolio contains of 3 stocks. Quantity, time of purchase and time of selling is known for each stock
- Stock prices of all 3 stocks in portfolio is given for 22 days
- We also know that first line of the stock prices belong to first stock, second line to second stock, so on and so forth
- We are interested in P/L position at the end of 5th day

**Example 2**

**Input**

3

10 4 20

10 1 11

100 6 0

22

113 115 112 113 115 112 113 115 112 113 115 112 113 115 112 113 115 112 113 115 112 117

52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73

101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122

20

**Output**

120

1400

**Explanation**

Till day 20 we have brought all the above listed stocks and only two have been matured that is stock number 1 and 2 therefore the total realized profit is 20 + 100 = 120 and stock number 3 has not been sold therefore the total unrealized profit is 1400.

From input we know the following

- Portfolio contains of 3 stocks. Quantity, time of purchase and time of selling is known for each stock
- Stock prices of all 3 stocks in portfolio is given for 22 days
- We also know that first line of the stock prices belong to first stock, second line to second stock, so on and so forth
- We are interested in P/L position at the end of 20th day

**Example 3**

**Input**

3

10 4 6

10 1 11

100 6 0

22

113 115 112 113 115 112 113 115 112 113 115 112 113 115 112 113 115 112 113 115 112 117

52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73

101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122

10

**Output**

-10

490

**Explanation**

From input we know the following

- Portfolio contains of 3 stocks. Quantity, time of purchase and time of selling is known for each stock
- Stock prices of all 3 stocks in portfolio is given for 22 days
- We also know that first line of the stock prices belong to first stock, second line to second stock, so on and so forth
- We are interested in P/L position at the end of 10th day

n = int(input()) stocks = [] for _ in range(n): a, b, c = map(int, input().split()) stocks.append((a, b, c)) m = int(input()) real = 0 unreal = 0 placementlelo = [] for i in range(n): arr = [int(i) for i in input().split()] placementlelo.append(arr) day = int(input()) for i in range(n): a, b, c = stocks[i] if b > day: continue elif c > day or not c: unreal += a * (placementlelo[i][day - 1] - placementlelo[i][b - 1]) else: real += a * (placementlelo[i][c - 1] - placementlelo[i][b - 1]) print(real) print(unreal, end="")