#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
#include <iomanip>

using namespace std;

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

    int N, D;
    cin >> N >> D;

    // TODO: Read portfolio weights
    vector<double> weights(N);
    for (int i = 0; i < N; ++i) {
        cin >> weights[i];
    }

    // TODO: Read asset returns and compute portfolio returns
    // You'll need to store returns for the correlation matrix calculation
    vector<vector<double>> returns(D, vector<double>(N));
    vector<double> portfolio_returns(D);

    for (int d = 0; d < D; ++d) {
        double portfolio_return = 0.0;
        for (int i = 0; i < N; ++i) {
            cin >> returns[d][i];
            portfolio_return += weights[i] * returns[d][i];
        }
        portfolio_returns[d] = portfolio_return;
    }

    // TODO: Compute statistics on portfolio returns
    // Mean daily return
    double mean_return = 0.0;
    for (int d = 0; d < D; ++d) {
        mean_return += portfolio_returns[d];
    }
    mean_return /= D;

    // Variance (or variance * D for batch formula)
    double variance = 0.0;
    for (int d = 0; d < D; ++d) {
        double diff = portfolio_returns[d] - mean_return;
        variance += diff * diff;
    }
    variance /= D;

    // TODO: Compute portfolio volatility (annualized)
    // Formula: daily_vol = sqrt(variance)
    //          annual_vol = daily_vol * sqrt(252)
    double daily_vol = sqrt(variance);
    double portfolio_volatility = daily_vol * sqrt(252.0);

    // TODO: Compute portfolio Sharpe ratio (annualized, rf=0)
    // Formula: sharpe = (mean_return * 252) / annual_vol
    double sharpe_ratio = (mean_return * 252.0) / portfolio_volatility;

    // TODO: Compute VaR 95% and VaR 99%
    // Sort portfolio returns, find percentiles
    // Output as positive loss (negate the percentile value)
    vector<double> sorted_returns = portfolio_returns;
    sort(sorted_returns.begin(), sorted_returns.end());

    double var_95 = 0.0;  // 5th percentile
    double var_99 = 0.0;  // 1st percentile

    // TODO: Compute CVaR 95% and CVaR 99%
    // Average of all returns at or below VaR threshold
    double cvar_95 = 0.0;
    double cvar_99 = 0.0;

    // TODO: Compute maximum drawdown
    // Track cumulative return, running peak, and max drawdown
    double max_drawdown = 0.0;

    // TODO: Compute correlation matrix
    // First compute covariance matrix from returns
    // Then convert to correlation using stds
    vector<vector<double>> correlation(N, vector<double>(N));

    // Output
    cout << fixed << setprecision(6);
    cout << portfolio_volatility << "\n";
    cout << sharpe_ratio << "\n";
    cout << var_95 << "\n";
    cout << var_99 << "\n";
    cout << cvar_95 << "\n";
    cout << cvar_99 << "\n";
    cout << max_drawdown << "\n";

    // Output upper triangle of correlation matrix
    for (int i = 0; i < N; ++i) {
        for (int j = i; j < N; ++j) {
            if (j > i) cout << " ";
            cout << correlation[i][j];
        }
        cout << "\n";
    }

    return 0;
}
