## 1) Concise, abridged problem statement

You are given a portfolio of **N assets** (1 ≤ N ≤ 10) with known **quantities** and **historical prices** for **T+1 days** (1 ≤ T ≤ 10⁴): day 0 is today, days 1..T are previous days.

For each asset *i* and day *t (1..T)*, define the (simple) return:
\[
r_t(i) = \frac{price_{t-1}(i) - price_t(i)}{price_t(i)}
\]
Compute:
- Today's asset values \(V_i = qty_i \cdot price_0(i)\)
- Portfolio value \(V_P = \sum_i V_i\)
- Weights \(w_i = V_i / V_P\)
- Mean return per asset \(m_i\) as the average of \(r_t(i)\) over t=1..T
- Portfolio mean \(m_P = \sum_i w_i m_i\)
- Portfolio standard deviation \(s_P\) using the variance-covariance (VCV) model
- 95% VaR:
\[
VaR = -V_P \cdot (m_P - 1.644854 \cdot s_P)
\]

Output VaR rounded/printed to 2 decimals.

---

## 2) Detailed Editorial

### Key idea
The naive way is:
1. Compute all asset returns \(r_t(i)\)
2. Compute covariance matrix \(S\) (N×N):
\[
S_{ij}=\frac{1}{T}\sum_{t=1}^{T} (r_t(i)-m_i)(r_t(j)-m_j)
\]
3. Compute portfolio variance \(s_P^2 = W^T S W\)

But building \(S\) costs **O(T·N²)** (up to 10⁴·100 = 10⁶ ops, which is okay), yet the time limit is tiny (0.25s) and constant factors matter—especially with slow I/O and doubles.

### Optimization: compute portfolio variance directly in O(T·N)
We want:
\[
s_P^2 = W^T S W
\]
Expand using the definition of covariance:
\[
\begin{aligned}
W^T S W
&= \sum_{i,j} w_i S_{ij} w_j \\
&= \sum_{i,j} w_i w_j \cdot \frac{1}{T}\sum_{t=1}^{T}(r_t(i)-m_i)(r_t(j)-m_j) \\
&= \frac{1}{T}\sum_{t=1}^{T}\sum_{i,j}\big(w_i(r_t(i)-m_i)\big)\big(w_j(r_t(j)-m_j)\big) \\
&= \frac{1}{T}\sum_{t=1}^{T}\left(\sum_i w_i(r_t(i)-m_i)\right)^2
\end{aligned}
\]

Define the **demeaned portfolio return** for day t:
\[
d_t = \sum_i w_i(r_t(i)-m_i)
\]
Then:
\[
s_P^2 = \frac{1}{T}\sum_{t=1}^{T} d_t^2
\]

So we never need the NxN covariance matrix.

### Steps
1. **Read inputs**: T, N, quantities, then prices for t=0..T.
2. **Compute today's portfolio weights**:
   - \(V_i = qty_i \cdot price_0(i)\)
   - \(V_P = \sum V_i\)
   - \(w_i = V_i / V_P\)
3. **Compute mean return per asset**:
   - For each t=1..T:
     - compute \(r_t(i)\) using prices[t-1][i] and prices[t][i]
     - accumulate sum[i] += r_t(i)
   - \(m_i = sum[i]/T\)
4. **Compute portfolio mean**:
   - \(m_P = \sum_i w_i m_i\)
5. **Compute portfolio variance directly**:
   - var = (1/T) * Σ_t ( Σ_i w_i (r_t(i) - m_i) )²
6. **Compute VaR**:
   - \(s_P = \sqrt{var}\)
   - \(VaR = -V_P (m_P - 1.644854 s_P)\)
7. Print with 2 decimals.

### Notes on performance
- T can be 10⁴, N ≤ 10, so O(T·N) is very small.
- The main bottleneck is **fast input parsing** of many doubles; the provided C++ uses a custom buffered reader.

---

## 3) C++ Solution

```cpp
#include <bits/stdc++.h>

using namespace std;

char buf[1 << 20];
int pos, len;

inline char gc() {
    if(pos == len) {
        len = fread(buf, 1, sizeof(buf), stdin);
        pos = 0;
    }
    return pos < len ? buf[pos++] : EOF;
}

inline int read_int() {
    int x = 0;
    char c;
    while((c = gc()) < '0')
        ;
    do {
        x = x * 10 + c - '0';
    } while((c = gc()) >= '0');
    return x;
}

inline double read_double() {
    int x = 0;
    char c;
    while((c = gc()) < '0')
        ;
    do {
        x = x * 10 + c - '0';
    } while((c = gc()) >= '0');
    if(c == '.') {
        int frac = 0, div = 1;
        while((c = gc()) >= '0') {
            frac = frac * 10 + c - '0';
            div *= 10;
        }
        return x + (double)frac / div;
    }
    return x;
}

int T, N;
vector<int> qty;
vector<vector<double>> price;

void read() {
    T = read_int();
    N = read_int();
    qty.resize(N);
    for(int i = 0; i < N; i++) {
        qty[i] = read_int();
    }
    price.resize(T + 1, vector<double>(N));
    for(int t = 0; t <= T; t++) {
        for(int i = 0; i < N; i++) {
            price[t][i] = read_double();
        }
    }
}

void solve() {
    // This is more of an implementation problem, as there is a full description
    // of what we want to do. Naively following, we get a O(N^2 * T)  solution
    // which might have to be optimized to pass.
    //
    // We need W'SW where S is the covariance matrix. Instead of
    // building S explicitly (O(N^2 * T)), we can notice that the W'SW risk of
    // the portfolio is just the variance of the portfolio return. The portfolio
    // return at time t is sum of w[i] * r[t][i], i.e. the weighted sum of asset
    // returns. So we compute this weighted return for each day, subtract its
    // mean, square, and average. This gives us the portfolio variance directly
    // in O(N * T).
    //
    // Formally: S[i][j] = (1/T) * sum_t (r[t][i] - m[i]) * (r[t][j] - m[j])
    //
    // W'SW = sum_{i,j} w[i] * S[i][j] * w[j]
    //      = sum_{i,j} w[i] * w[j] * (1/T) * sum_t (r[t][i]-m[i])*(r[t][j]-m[j]) 
    //      = (1/T) * sum_t sum_{i,j} w[i]*(r[t][i]-m[i]) * w[j]*(r[t][j]-m[j]) 
    //      = (1/T) * sum_t (sum_i w[i]*(r[t][i]-m[i]))^2 
    //      = (1/T) * sum_t dot[t]^2
    //
    // where dot[t] = sum_i w[i] * (r[t][i] - m[i]) is the demeaned portfolio
    // return.
    //
    // We might also need to implement fast reading to make this problem pass on
    // codeforces.

    double VP = 0;
    vector<double> w(N);
    for(int i = 0; i < N; i++) {
        w[i] = qty[i] * price[0][i];
        VP += w[i];
    }
    for(int i = 0; i < N; i++) {
        w[i] /= VP;
    }

    vector<double> sum(N);
    for(int t = 1; t <= T; t++) {
        for(int i = 0; i < N; i++) {
            sum[i] += (price[t - 1][i] - price[t][i]) / price[t][i];
        }
    }
    vector<double> m(N);
    double mP = 0;
    for(int i = 0; i < N; i++) {
        m[i] = sum[i] / T;
        mP += w[i] * m[i];
    }

    double var = 0;
    for(int t = 1; t <= T; t++) {
        double dot = 0;
        for(int i = 0; i < N; i++) {
            double r = (price[t - 1][i] - price[t][i]) / price[t][i] - m[i];
            dot += w[i] * r;
        }
        var += dot * dot;
    }
    var /= T;

    double sP = sqrt(var);
    double VaR = -VP * (mP - 1.644854 * sP);

    printf("%.2f\n", VaR);
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int T = 1;
    // cin >> T;
    for(int test = 1; test <= T; test++) {
        read();
        // cout << "Case #" << test << ": ";
        solve();
    }

    return 0;
}
```

---

## 4) Python solution (same algorithm) with detailed comments

```python
import sys
import math

def solve() -> None:
    data = sys.stdin.buffer.read().split()
    it = iter(data)

    # Read T (number of returns) and N (number of assets)
    T = int(next(it))
    N = int(next(it))

    # Quantities of each asset
    qty = [int(next(it)) for _ in range(N)]

    # Prices for days 0..T (T+1 rows), N columns each
    # price[t][i] is price of asset i at day t
    price = [[0.0] * N for _ in range(T + 1)]
    for t in range(T + 1):
        for i in range(N):
            price[t][i] = float(next(it))

    # ----- Step 1: compute portfolio value VP and weights w -----
    # V_i = qty_i * price_0(i), VP = sum V_i, w_i = V_i / VP
    values = [qty[i] * price[0][i] for i in range(N)]
    VP = sum(values)
    w = [v / VP for v in values]

    # ----- Step 2: compute mean return per asset m[i] -----
    # r_t(i) = (price_{t-1}(i) - price_t(i)) / price_t(i)
    sums = [0.0] * N
    for t in range(1, T + 1):
        prev_row = price[t - 1]
        cur_row = price[t]
        for i in range(N):
            sums[i] += (prev_row[i] - cur_row[i]) / cur_row[i]

    m = [s / T for s in sums]                  # mean return per asset
    mP = sum(w[i] * m[i] for i in range(N))    # mean return of portfolio

    # ----- Step 3: compute portfolio variance directly in O(T*N) -----
    # var = (1/T) * sum_t ( sum_i w_i * (r_t(i) - m_i) )^2
    var = 0.0
    for t in range(1, T + 1):
        prev_row = price[t - 1]
        cur_row = price[t]

        demeaned_portfolio_return = 0.0
        for i in range(N):
            r = (prev_row[i] - cur_row[i]) / cur_row[i]  # r_t(i)
            demeaned_portfolio_return += w[i] * (r - m[i])

        var += demeaned_portfolio_return * demeaned_portfolio_return

    var /= T
    sP = math.sqrt(var)

    # ----- Step 4: compute VaR (95% z = 1.644854) -----
    VaR = -VP * (mP - 1.644854 * sP)

    # Print with 2 decimals
    sys.stdout.write(f"{VaR:.2f}\n")

if __name__ == "__main__":
    solve()
```

---

## 5) Compressed editorial

- Compute today's portfolio value \(V_P=\sum qty_i\cdot price_0(i)\) and weights \(w_i=V_i/V_P\).
- For each day \(t=1..T\), compute asset returns \(r_t(i)=\frac{price_{t-1}(i)-price_t(i)}{price_t(i)}\).
- Compute mean returns \(m_i=\frac{1}{T}\sum_t r_t(i)\) and portfolio mean \(m_P=\sum_i w_i m_i\).
- Avoid covariance matrix: portfolio variance equals variance of the weighted demeaned return:
  \[
  s_P^2=\frac{1}{T}\sum_{t=1}^{T}\left(\sum_i w_i(r_t(i)-m_i)\right)^2
  \]
- Then \(VaR=-V_P(m_P-1.644854\,s_P)\). Output with 2 decimals.
