Problem:
森林伐採 (Deforestation)
問題文
JOI 王国には広大な森林がある．森林は長方形の形をしており，南北に
H
マス，東西に
W
マスのマス目状に分けられている．北から
i
マス目，西から
j
マス目(
1 ≦ i ≦ H, 1 ≦ j ≦ W
)の領域には
A_{i,j}
本の木が生えている．ただし，北西の端の領域には木材加工工場があり，木が生えていない．すなわち，
A_{1,1}=0
である．
木が生えていない領域には人が立ち入ることが出来る．また人は東西南北に隣接する領域に，その領域に木が生えていなければ，移動することができる．森林の外に出ることはできない．JOI 君は JOI 王国の公共事業として，木を伐採し，北西の端の領域と南東の端の領域を，相互に行き来可能にしたい．
木の伐採は以下のようにして行う．はじめ，JOI 君は木材加工工場のある北西の端の領域にいる．JOI 君は，現在いる領域と東西南北に隣接する木の生えていない領域に
1
分で移動することができる．また，東西南北に隣接する木の生えている領域から，
1
分で木を
1
本伐採することができる．ただし，木を
1
本伐採したら，そのたびに北西の端の領域にある木材加工工場まで伐採した木を運ばなければならない．木を運んでいる間も，JOI 君の移動速度は変わらない．木を運んでいる間は，他の木を伐採することはできない．
条件を満たすように木を伐採するのにかかる時間の最小値を求めよ．ただし，伐採にかかる時間とは，最後に伐採した木を，木材加工工場に運ぶまでの時間とする．
制約
1 \leq  H  \leq 30
1 \leq  W  \leq 30
(H, W) ≠ (1, 1)
0 \leq A_{i,j} ≦ 10000
(
1 \leq i \leq H, 1 \leq j \leq W
)
A_{1,1}=0
入力・出力
入力
入力は以下の形式で標準入力から与えられる．
H
W
A_{1,1}
...
A_{1,W}
:
A_{H,1}
...
A_{H,W}
出力
条件を満たすように木を伐採するのにかかる時間の最小値を
1
行で出力せよ．
入出力例
入力例 1
2 3
0 1 2
3 4 5
出力例 1
32
北から
i
マス目，西から
j
マス目の領域を
(i, j)
で表す．
まず
(1, 2)
の木を伐採する．これには
1
分かかる．
次に
(1, 3)
の木をすべて伐採する．
1
本伐採するのに，
(1,1)
から東に
1
マス進み，
(1, 3)
の木を伐採し，西に
1
マス進んで
(1,1)
に戻ればいいので，
3
分かかる．よってこれには
2 × 3 = 6
分かかる．
次に
(2, 3)
の木をすべて伐採する．
1
本伐採するのに，
(1,1)
から東に
2
マス進み，
(2, 3)
の木を伐採し，西に
2
マス進んで
(1,1)
に戻ればいいので，
5
分かかる．よってこれには
5 × 5 = 25
分かかる．
全部で
1 + 6 + 25 = 32
分かかる．これより少ない時間で，条件を満たすように木を伐採することはできないので，
32
を出力する．
入力例 2
2 5
0 5 0 0 0
0 0 0 9 1
出力例 2
13
(2, 5)
の木のみを伐採すればよい．
入力例 3
2 5
0 2 0 0 0
0 0 0 9 1
出力例 3
11
まず，
(1, 2)
の木を伐採して，次に
(2, 5)
の木を伐採すればよい．
情報オリンピック日本委員会作 『第 17 回日本情報オリンピック JOI 2017/2018 予選競技課題』

Submitted solutions:

code_snippet: #include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int INF = 1e9;
const ll LINF = 1e18;
template<class S,class T> ostream& operator << (ostream& out,const pair<S,T>& o){ out << "(" << o.first << "," << o.second << ")"; return out; }
template<class T> ostream& operator << (ostream& out,const vector<T> V){ for(int i = 0; i < V.size(); i++){ out << V[i]; if(i!=V.size()-1) out << " ";} return out; }
template<class T> ostream& operator << (ostream& out,const vector<vector<T> > Mat){ for(int i = 0; i < Mat.size(); i++) { if(i != 0) out << endl; out << Mat[i];} return out; }
template<class S,class T> ostream& operator << (ostream& out,const map<S,T> mp){ out << "{ "; for(auto it = mp.begin(); it != mp.end(); it++){ out << it->first << ":" << it->second; if(mp.size()-1 != distance(mp.begin(),it)) out << ", "; } out << " }"; return out; }

/*
 <url:>
 問題文============================================================
 =================================================================
 解説=============================================================
 ================================================================
 */

int d[4] = {1,0,-1,0};
ll H,W;
using Items1 = tuple<ll,ll,ll>;
using Items2 = tuple<ll,ll,ll,ll>;
map<Items1,ll> mpT;
ll solve(){
    ll res = LINF;
    cin >> H >> W;
    vector<vector<ll>> masu(H+2,vector<ll>(W+2,INF));
    for(int i = 1; i <= H;i++){
        for(int j = 1; j <= W;j++){
            cin >> masu[i][j];
        }
    }
    vector<vector<ll>> dist(H+2,vector<ll>(W+2,INF));
    mpT[Items1(1,1,0)] = 1;
    priority_queue<Items2> q; q.push(Items2(0,1,1,0));
    while(q.size()){
        ll x,y,D,T; tie(T,x,y,D) = q.top(); q.pop();
        if(D > H*W) continue;
        T = -T;
        for(int k = 0; k < 4;k++){
            ll nx = x + d[k],ny = y + d[k^1];
            if(masu[nx][ny] == INF) continue;
            ll Add = masu[nx][ny] + masu[nx][ny]*D*2 + 1;
            if(nx == H && ny == W){
                res = min(res,T + Add - D - 1);
            //    return res;
                continue;
            }
            if(mpT.count(Items1(nx,ny,D+1))){
                if(mpT[Items1(nx,ny,D+1)] <= T + Add){
                    continue;
                }
            }
            mpT[Items1(nx,ny,D+1)] = T + Add;
            q.push(Items2(-(T+Add),nx,ny,D+1));
        }
    }
    return res;
}
int main(void) {
    cin.tie(0); ios_base::sync_with_stdio(false);
    cout << solve() << endl;
    return 0;
}
accuracy: 1
time_ms: 580
memory_kb: 38360
final_rank: 19

code_snippet: #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> pi;
typedef pair<ll,pi> ppi;
typedef pair<ll,ppi> pppi;
typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
#define FOR(i,l,r) for(ll i=l;i<r;i++)
#define REP(i,n) FOR(i,0,n)
#define ALL(A) A.begin(),A.end()
#define LB(A,x) (lower_bound(ALL(A),x)-A.begin())
template<typename T>bool chmin(T&a,T b){if(a>b){a=b;return 1;}return 0;}
int main(){
    int H,W;cin>>H>>W;
    vector<vector<ll>>A(H,vector<ll>(W));
    REP(i,H)REP(j,W)cin>>A[i][j];
    vvvi D(H,vvi(W,vi(1000,1e18)));
    D[0][0][0]=0;
    priority_queue<pppi,vector<pppi>,greater<pppi>>Q;
    Q.emplace(pppi(0,ppi(0,pi(0,0))));
    while(!Q.empty()){
        auto[d,p]=Q.top();Q.pop();
        auto[x,_p]=p;
        auto[y,c]=_p;
        if(d!=D[x][y][c])continue;
        d++;
        if(x&&chmin(D[x-1][y][c+1],d+A[x-1][y]*(1+2*c)))Q.emplace(pppi(D[x-1][y][c+1],ppi(x-1,pi(y,c+1))));
        if(x+1<H&&chmin(D[x+1][y][c+1],d+A[x+1][y]*(1+2*c)))Q.emplace(pppi(D[x+1][y][c+1],ppi(x+1,pi(y,c+1))));
        if(y&&chmin(D[x][y-1][c+1],d+A[x][y-1]*(1+2*c)))Q.emplace(pppi(D[x][y-1][c+1],ppi(x,pi(y-1,c+1))));
        if(y+1<W&&chmin(D[x][y+1][c+1],d+A[x][y+1]*(1+2*c)))Q.emplace(pppi(D[x][y+1][c+1],ppi(x,pi(y+1,c+1))));
    }
    ll ans=1e18;
    REP(c,1000)chmin(ans,D[H-1][W-1][c]-c);
    cout<<ans<<endl;
    return 0;
}
accuracy: 1
time_ms: 70
memory_kb: 19996
final_rank: 16

code_snippet: #include<bits/stdc++.h>
using namespace std;

using ll = long long;

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

    ll H,W;
    cin>>H>>W;
    vector<vector<ll>> A(H,vector<ll>(W));
    for(int h=0;h<H;h++)for(int w=0;w<W;w++)cin>>A[h][w];
    vector<vector<vector<ll>>> DP(H,vector<vector<ll>>(W,vector<ll>(H*W+1,1e18)));
    DP[0][0][0]=0;
    vector<int> dx={1,0,-1,0},dy={0,1,0,-1};
    for(int d=0;d<H*W;d++){
        for(int h=0;h<H;h++){
            for(int w=0;w<W;w++){
                for(int e=0;e<4;e++){
                    int nh=h+dy[e];
                    int nw=w+dx[e];
                    if(nh<0||nw<0||nh>=H||nw>=W)continue;
                    DP[nh][nw][d+1]=min(DP[nh][nw][d+1],DP[h][w][d]+1+(2*d+1)*A[nh][nw]);
                }
            }
        }
    }
    ll an=1e18;
    for(int d=0;d<H*W;d++)an=min(an,DP[H-1][W-1][d]-d);
    cout<<an<<endl;
}
accuracy: 1
time_ms: 10
memory_kb: 9752
final_rank: 3

code_snippet: /* -*- coding: utf-8 -*-
 *
 * 0645.cc: 森林伐採 (Deforestation)
 */

#include<cstdio>
#include<queue>
#include<algorithm>
#include<utility>
 
using namespace std;

/* constant */

const int MAX_H = 30;
const int MAX_W = 30;
const int MAX_HW = MAX_H * MAX_W;
const int MAX_GN = MAX_HW * MAX_HW;
const long long LINF = 1LL << 62;

const int dys[] = { 0, 1, 0, -1 }, dxs[] = { 1, 0, -1, 0 };

/* typedef */

typedef long long ll;
typedef pair<ll,int> pli;

/* global variables */

int as[MAX_H][MAX_W];
ll ds[MAX_GN];

/* subroutines */

/* main */

int main() {
  int h, w;
  scanf("%d%d", &h, &w);
  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++) scanf("%d", as[i] + j);

  int hw = h * w, gn = hw * hw;
  fill(ds, ds + gn, LINF);
  ds[0] = 0;

  priority_queue<pli> q;
  q.push(pli(0, 0));

  ll mind = LINF;
  while (! q.empty()) {
    pli ue = q.top(); q.pop();
    ll ud = -ue.first;
    int u = ue.second;
    if (ds[u] != ud) continue;

    int up = u / hw, umd = u % hw;
    int uy = up / w, ux = up % w;
    //printf("%d,%d,%d = %lld\n", uy, ux, umd, ud);
    if (up == hw - 1) { mind = ud; break; }
    if (umd == hw - 1) continue;

    for (int di = 0; di < 4; di++) {
      int vy = uy + dys[di], vx = ux + dxs[di];
      if (vy >= 0 && vy < h && vx >= 0 && vx < w) {
	ll vd = ud + (ll)as[vy][vx] * (1 + umd * 2);
	int v = (vy * w + vx) * hw + (umd + 1);
	if (ds[v] > vd) {
	  ds[v] = vd;
	  q.push(pli(-vd, v));
	}
      }
    }
  }

  printf("%lld\n", mind);
  return 0;
}
accuracy: 1
time_ms: 40
memory_kb: 15100
final_rank: 9

code_snippet: #include <bits/stdc++.h>
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define drep(i,j,n) for(int i=0;i<(int)(n-1);i++)for(int j=i+1;j<(int)(n);j++)
#define trep(i,j,k,n) for(int i=0;i<(int)(n-2);i++)for(int j=i+1;j<(int)(n-1);j++)for(int k=j+1;k<(int)(n);k++)
#define codefor int test;scanf("%d",&test);while(test--)
#define INT(...) int __VA_ARGS__;in(__VA_ARGS__)
#define LL(...) ll __VA_ARGS__;in(__VA_ARGS__)
#define yes(ans) if(ans)printf("yes\n");else printf("no\n")
#define Yes(ans) if(ans)printf("Yes\n");else printf("No\n")
#define YES(ans) if(ans)printf("YES\n");else printf("NO\n")
#define popcount(v) __builtin_popcount(v)
#define vector2d(type,name,h,...) vector<vector<type>>name(h,vector<type>(__VA_ARGS__))
#define vector3d(type,name,h,w,...) vector<vector<vector<type>>>name(h,vector<vector<type>>(w,vector<type>(__VA_ARGS__)))
#define umap unordered_map
#define uset unordered_set
using namespace std;
using ll = long long;
template<class T> using rpriority_queue = priority_queue<T, vector<T>, greater<T>>;
const int MOD=1000000007;
const int MOD2=998244353;
const int INF=1<<30;
const ll INF2=(ll)1<<60;
void scan(int& a){scanf("%d",&a);}
void scan(long long& a){scanf("%lld",&a);}
template<class T,class L>void scan(pair<T, L>& p){scan(p.first);scan(p.second);}
template<class T,class U,class V>void scan(tuple<T,U,V>& p){scan(get<0>(p));scan(get<1>(p));scan(get<2>(p));}
template<class T> void scan(T& a){cin>>a;}
template<class T> void scan(vector<T>& vec){for(auto&& it:vec)scan(it);}
void in(){}
template <class Head, class... Tail> void in(Head& head, Tail&... tail){scan(head);in(tail...);}
void print(const int& a){printf("%d",a);}
void print(const long long& a){printf("%lld",a);}
void print(const double& a){printf("%.15lf",a);}
template<class T,class L>void print(const pair<T, L>& p){print(p.first);putchar(' ');print(p.second);}
template<class T> void print(const T& a){cout<<a;}
template<class T> void print(const vector<T>& vec){if(vec.empty())return;print(vec[0]);for(auto it=vec.begin();++it!= vec.end();){putchar(' ');print(*it);}}
void out(){putchar('\n');}
template<class T> void out(const T& t){print(t);putchar('\n');}
template <class Head, class... Tail> void out(const Head& head,const Tail&... tail){print(head);putchar(' ');out(tail...);}
template<class T> void dprint(const T& a){cerr<<a;}
template<class T> void dprint(const vector<T>& vec){if(vec.empty())return;cerr<<vec[0];for(auto it=vec.begin();++it!= vec.end();){cerr<<" "<<*it;}}
void debug(){cerr<<endl;}
template<class T> void debug(const T& t){dprint(t);cerr<<endl;}
template <class Head, class... Tail> void debug(const Head& head, const Tail&... tail){dprint(head);cerr<<" ";debug(tail...);}
ll intpow(ll a, ll b){ ll ans = 1; while(b){ if(b & 1) ans *= a; a *= a; b /= 2; } return ans; }
ll modpow(ll a, ll b, ll p){ ll ans = 1; while(b){ if(b & 1) (ans *= a) %= p; (a *= a) %= p; b /= 2; } return ans; }
ll modinv(ll a, ll m) {ll b = m, u = 1, v = 0;while (b) {ll t = a / b;a -= t * b; swap(a, b);u -= t * v; swap(u, v);}u %= m;if (u < 0) u += m;return u;}
ll updivide(ll a,ll b){if(a%b==0) return a/b;else return (a/b)+1;}
template<class T> void chmax(T &a,const T b){if(b>a)a=b;}
template<class T> void chmin(T &a,const T b){if(b<a)a=b;}

int main(){
    INT(h,w);
    vector2d(int,A,h,w);
    in(A);
    vector3d(ll,dist,h,w,h*w,INF2);
    rpriority_queue<tuple<ll,int,int,int>> pq;
    dist[0][0][0]=0;
    pq.push({0,0,0,0});
    int y,x,t;
    ll c;
    while(!pq.empty()){
        tie(c,y,x,t)=pq.top();
        pq.pop();
        if(t+1==h*w)continue;
        for(int i=0;i<4;i++){
            int dy=(i==0)-(i==1);
            int dx=(i==2)-(i==3);
            if(y+dy<0||y+dy>=h||x+dx<0||x+dx>=w)continue;
            if(c+(ll)(2*t+1)*A[y+dy][x+dx]<dist[y+dy][x+dx][t+1]){
                dist[y+dy][x+dx][t+1]=c+(ll)(2*t+1)*A[y+dy][x+dx];
                pq.push({dist[y+dy][x+dx][t+1],y+dy,x+dx,t+1});
            }
        }
    }
    ll ans=INF2;
    rep(i,h*w)chmin(ans,dist[h-1][w-1][i]);
    out(ans);
}
accuracy: 1
time_ms: 50
memory_kb: 17212
final_rank: 11

code_snippet: #pragma region my_template
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;
using ll = long long;
using pi = pair<int, int>;
using pl = pair<ll, ll>;
using vi = vector<int>;
using vvi = vector<vi>;
using vl = vector<ll>;
using vvl = vector<vl>;

#define range(i, l, r) for(int i = (int)(l); i < (int)(r); i++)
#define rrange(i, l, r) for(int i = (int)(r)-1; i >= (int)(l); i--)
#define rep(i, n) range(i, 0, n)
#define rrep(i, n) rrange(i, 0, n)
#define len(a) ((int)(a).size())
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define uni(a) (a).erase(unique(all(a)), (a).end());
#define append push_back
#define mp make_pair
#define elif else if

constexpr int MOD = 1e9+7;
//constexpr int MOD = 998244353;
constexpr int INF = 1e9;
constexpr ll LINF = 1e18;

template<typename T>
istream &operator >> (istream &in, vector<T> &a){
    for(T &x: a) in >> x;
    return in;
}
template<typename T, typename U>
istream &operator >> (istream &in, pair<T, U> &a){
    in >> a.first >> a.second;
    return in;
}
template<typename T>
ostream &operator << (ostream &out, const vector<T> &a) {
    rep(i, len(a)) out << a[i] << (i == len(a)-1 ? "" : " ");
    return out;
}
template<typename T, typename U>
ostream &operator << (ostream &out, const pair<T, U> &a){
    out << a.first << " " << a.second;
    return out;
}
inline void print() { cout << "\n"; }
template <typename T, typename ...U>
inline void print(const T &t, const U &...u) {
    cout << t;
    if (sizeof...(u)) cout << " ";
    print(u...);
}
template<typename T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<typename T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return 1; } return 0; }
#pragma endregion

vector<pi> arr4 = {{-1, 0}, {0, -1}, {1, 0}, {0, 1}};

ll dp[910][31][31];
void solve() {
    int h, w; cin >> h >> w;
    int n = h*w;
    vvl a(h, vl(w)); cin >> a;
    rep(i, 910) rep(j, 31) rep(k, 31) dp[i][j][k] = LINF;
    dp[0][0][0] = 0;

    rep(i, n) rep(y, h) rep(x, w) {
        if (dp[i][y][x] == LINF) continue;
        chmin(dp[i+1][y][x], dp[i][y][x]);
        for (auto [dy, dx]: arr4) {
            int ny = y+dy, nx = x+dx;
            if (ny < 0 or nx < 0 or ny >= h or nx >= w) continue;
            chmin(dp[i+1][ny][nx], dp[i][y][x] + (i*2+1)*a[ny][nx]);
        }
    }

    print(dp[n-1][h-1][w-1]);
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    //cout << fixed << setprecision(15);
    solve();
    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 10308
final_rank: 4

code_snippet: // clang-format off
#include <bits/stdc++.h>
using namespace std;
using ll = int64_t;
using ld = long double;
using Pi = pair<int, int>;
using Pl = pair<ll, ll>;
using Vi = vector<int>;
using Vl = vector<ll>;
#define FOR(i, m, n) for(int i = (m); i < (n); ++i)
#define FORR(i, m, n) for(int i = (m)-1; i >= (n); --i)
#define rep(i, n) FOR(i, 0, n)
#define repn(i, n) FOR(i, 1, n+1)
#define repr(i, n) FORR(i, n, 0)
#define repnr(i, n) FORR(i, n+1, 1)
#define all(s) (s).begin(), (s).end()
template <class T, class U>
bool chmax(T &a, const U b) { return a < b ? a = b, true : false; }
template <class T, class U>
bool chmin(T &a, const U b) { return b < a ? a = b, true : false; }
template <class T>
istream &operator>>(istream &is, vector<T> &v) { for (T &i : v) is >> i; return is; }
template <class T>
ostream &operator<<(ostream &os, const vector<T> &v) { for (const T &i : v) os << i << ' '; return os; }
template <class Head, class... Tail>
void co(Head&& head, Tail&&... tail) {
    if constexpr(sizeof...(tail)==0) cout<<head<<'\n'; else cout<<head<<' ',co(forward<Tail>(tail)...);
}
template <class Head, class... Tail>
void ce(Head&& head, Tail&&... tail) {
    if constexpr(sizeof...(tail)==0) cerr<<head<<'\n'; else cerr<<head<<' ',ce(forward<Tail>(tail)...);
}
template<typename T, typename... Args>
auto make_vector(T x, int arg, Args ...args) {
    if constexpr(sizeof...(args)==0) return vector<T>(arg, x); else return vector(arg,make_vector<T>(x, args...));
}
void sonic() { ios::sync_with_stdio(false); cin.tie(nullptr); }
void setp(const int n) { cout << fixed << setprecision(n); }
constexpr int64_t INF = 1000000000000000003;
constexpr int Inf = 1000000003;
constexpr int MOD = 1000000007;
constexpr int MOD_N = 998244353;
constexpr double EPS = 1e-7;
const double PI = acos(-1);

// clang-format on

struct Pos {
    int x, y;

    Pos &operator+=(const Pos &rhs) {
        x += rhs.x, y += rhs.y;
        return *this;
    }
};

vector<Pos> dv = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};

struct S {
    int x, y, d;

    bool operator<(const S &rhs) const {
        return d > rhs.d;
    }
};

int main(void) {
    int h, w;
    cin >> h >> w;
    auto a = make_vector(0, h, w);
    cin >> a;

    auto in_board = [&](int x, int y) {
        return x >= 0 && x < h && y >= 0 && y < w;
    };

    auto dp = make_vector(Inf, h, w, 1000);
    dp[0][0][0] = 0;
    priority_queue<S> pq;
    pq.push(S{0, 0, 0});
    while (!pq.empty()) {
        auto [x, y, d] = pq.top();
        pq.pop();
        for (auto p : dv) {
            Pos q = {x, y};
            q += p;
            if (!in_board(q.x, q.y))
                continue;
            if (chmin(dp[q.x][q.y][d + 1], dp[x][y][d] + a[q.x][q.y] * (2 * d + 1))) {
                pq.push({q.x, q.y, d + 1});
            }
        }
    }
    co(*min_element(all(dp[h - 1][w - 1])));

    return 0;
}
accuracy: 1
time_ms: 40
memory_kb: 6904
final_rank: 2

code_snippet: #include <bits/stdc++.h>
using namespace std;
#pragma GCC target("avx")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#define all(x) (x).begin(),(x).end()
#define rep(i,n) for(int (i)=0;i<(n);i++)
#define rrep(i,n) for(int (i)=1;i<=(n);i++)
#define REP(i,m,n) for(int (i)=(m);(i)<(n);(i)++)
#define MOD 1000000007
#define INF 1e18
#define int long long
#define endl "\n"
#define yorn(f) puts((f)?"Yes":"No")
#define YORN(f) puts((f)?"YES":"NO")
typedef long long ll;
typedef pair<int, int> P;
int gcd(int a,int b){return b?gcd(b,a%b):a;};
int lcm(int a,int b){return a/gcd(a,b)*b;};
int mod(int a,int b){return (a+b-1)/b;};
template<typename A, size_t N, typename T>
void Fill(A(&array)[N],const T &val){std::fill((T*)array,(T*)(array+N),val);}
template<class T>inline bool chmax(T& a,T b){if(a<b){a=b;return true;}return false;};
template<class T>inline bool chmin(T& a,T b){if(a>b){a=b;return true;}return false;};

int h, w;
int a[50][50];
int dp[50][50][2500];

int dx[] = {-1, 0, 1, 0};
int dy[] = {0, -1, 0, 1};

signed main() {
	cin.tie(nullptr);
	cout.tie(nullptr);
	ios::sync_with_stdio(false);
	//cout << fixed << setprecision(15);
	
	cin >> h >> w;
	rep(i, h) {
		rep(j, w) cin >> a[i][j];
	}
	Fill(dp, MOD);
	dp[0][0][0] = 0;
	rep(i, h*w) {
		rep(y, h) {
			rep(x, w) {
				rep(d, 4) {
					int nx = x + dx[d];
					int ny = y + dy[d];
					if(nx < 0 || ny < 0 || w <= nx || h <= ny) continue;
					chmin(dp[ny][nx][i+1], dp[y][x][i] + a[ny][nx]*(2*i+1));
				}
			}
		}
	}
	int ans = INF;
	rep(i, h*w) {
		chmin(ans, dp[h-1][w-1][i]);
	}
	cout << ans << endl;
	return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 52064
final_rank: 18

code_snippet: #include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <numeric>
#include <iomanip>
#include <stack>
#include <tuple>
#include <complex>

using namespace std;

using ll = long long;

constexpr ll infll = 1LL << 60;

constexpr int dx[] = { -1, 0, 1, 0 };
constexpr int dy[] = { 0, 1, 0, -1 };

int main() {

    int h, w;
    cin >> h >> w;
    vector<vector<int>> a(h, vector<int>(w));
    for (int i = 0; i < h; ++i) {
        for (int j = 0; j < w; ++j) {
            cin >> a[i][j];
        }
    }


    // dist[i][j][k] := (i, j) のマスに k 距離で行けるようにする最小コスト
    // 拡張ダイクストラ

    constexpr int kMax = 1000;

    vector dp(h, vector(w, vector(h * w + 1, infll)));
    dp[0][0][0] = 0;

    using T = tuple<ll, int, int, int>;
    priority_queue<T, vector<T>, greater<T>> que;
    que.emplace(0, 0, 0, 0);

    while (que.size()) {
        auto [cost, x, y, dist] = que.top();
        que.pop();

        if (dp[y][x][dist] < cost) continue;

        for (int k = 0; k < 4; ++k) {
            int ty = y + dy[k];
            int tx = x + dx[k];

            if (ty < 0 || ty >= h) continue;
            if (tx < 0 || tx >= w) continue;

            if (dist >= w * h) continue;

            ll new_cost = cost + a[ty][tx] * dist * 2 + 1 + a[ty][tx];

            if (dp[ty][tx][dist + 1] > new_cost) {
                //cout << ty << " " << tx << " " << dist + 1 << endl;
                dp[ty][tx][dist + 1] = new_cost;
                que.emplace(new_cost, tx, ty, dist + 1);
            }
        }
    }

    ll ans = infll;
    for (int i = 0; i < h * w + 1; ++i) {
        //out << dp[h - 1][w - 1][i] << endl;
        ans = min(ans, dp[h - 1][w - 1][i] - i);
    }

    cout << ans << endl;

}
accuracy: 1
time_ms: 60
memory_kb: 16976
final_rank: 12

code_snippet: #include<bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int,int>;
constexpr ll INF = 1e18;

using tup = tuple<ll,int,int,int>;

ll dp[910][35][35];

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    for(int i=0; i<910; i++){
        for(int j=0; j<35; j++){
            for(int k=0; k<35; k++){
                dp[i][j][k] = INF;
            }
        }
    }
    int H,W;
    cin >> H >> W;
    vector<vector<int>> G(H,vector<int>(W));
    for(int i=0; i<H; i++){
        for(int j=0; j<W; j++){
            cin >> G[i][j];
        }
    }
    vector<int> dx = {0,-1,0,1};
    vector<int> dy = {-1,0,1,0};
    priority_queue<tup,vector<tup>,greater<>> que;
    que.emplace(0,0,0,0);
    dp[0][0][0] = 0;
    while(que.size()){
        ll cost;
        int mn,y,x;
        tie(cost,mn,y,x) = que.top();
        que.pop();
        if(mn == 900) continue;
        if(dp[mn][y][x] < cost) continue;
        for(int k=0; k<4; k++){
            int ny = y + dy[k];
            int nx = x + dx[k];
            if(ny<0 || ny>=H || nx<0 || nx>=W) continue;
            ll ncost = (ll)G[ny][nx] * mn * 2 + G[ny][nx];
            if(dp[mn+1][ny][nx] > cost + ncost){
                dp[mn+1][ny][nx] = cost + ncost;
                que.emplace(dp[mn+1][ny][nx],mn+1,ny,nx);
            }
        }
    }
    ll ans = INF;
    for(int i=0; i<910; i++){
        if(ans > dp[i][H-1][W-1]){
            ans = dp[i][H-1][W-1];
        }
    }
    cout << ans << endl;
    return 0;
}
accuracy: 1
time_ms: 50
memory_kb: 19788
final_rank: 14

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define fast() ios::sync_with_stdio(false), cin.tie(nullptr)

int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};

using ll = long long;
vector<vector<vector<ll>>> dijkstra(int H, int W, const vector<vector<ll>> &a)
{
	const auto inf = numeric_limits<ll>::max();
	vector<vector<vector<ll>>> dist(H, vector<vector<ll>>(W, vector<ll>(1000, inf)));
	dist[0][0][0] = 0;
	using point = pair<int, int>;
	using T = pair<ll, pair<point, int>>;
	// 時間 位置 回数
	priority_queue<T, vector<T>, greater<T>> pq;
	pq.push({0, {{0, 0}, 0}});
	while (!pq.empty())
	{
		auto v = pq.top();
		ll time = v.first;
		int y, x, cnt = v.second.second;
		tie(y, x) = v.second.first;

		pq.pop();
		if (dist[y][x][cnt] < time)
			continue;

		if (cnt >= H * W)
			continue;

		for (int i = 0; i < 4; i++)
		{
			int ny = y + dy[i], nx = x + dx[i];
			if (ny < 0 || ny >= H || nx < 0 || nx >= W)
				continue;
			ll next_time = time + a[ny][nx] * 2 * cnt + 1 + a[ny][nx];
			if (dist[ny][nx][cnt + 1] <= next_time)
				continue;
			dist[ny][nx][cnt + 1] = next_time;
			pq.push({next_time, {{ny, nx}, cnt + 1}});
		}
	}
	return dist;
}

int main()
{
	int H, W;
	cin >> H >> W;
	vector<vector<ll>> a(H, vector<ll>(W));

	for (int i = 0; i < H; i++)
	{
		for (int j = 0; j < W; j++)
		{
			cin >> a[i][j];
		}
	}

	auto dist = dijkstra(H, W, a);
	ll ans = 1e18;
	for (int i = 0; i < 1000; i++)
	{
		ans = min(dist[H - 1][W - 1][i] - i, ans);
	}

	cout << ans << "\n";
}
accuracy: 1
time_ms: 70
memory_kb: 17892
final_rank: 13

code_snippet: #include <bits/stdc++.h>
using namespace std;

#define INF 1020304050
using T = tuple<int,int,int,int>;

int main() {
  
  ios::sync_with_stdio(false);
  cin.tie(0);
  int h,w;
  cin >> h >> w;
  vector<vector<int>> a(h,vector<int>(w));
  for (int i=0;i<h;i++) {
    for (int j=0;j<w;j++) {
      cin >> a[i][j];
    }
  }
  vector<vector<vector<int>>> ans(h,vector<vector<int>>(w,vector<int>(h*w+1,INF)));
  ans[0][0][0] = 0;
  priority_queue<T,vector<T>,greater<T>> pq;
  pq.push(make_tuple(0,0,0,0));
  while (!pq.empty()) {
    T nowt = pq.top();
    pq.pop();
    int val = get<0>(nowt);
    int nh = get<1>(nowt);
    int nw = get<2>(nowt);
    int dist = get<3>(nowt);
    if (dist == h*w) continue;
    if (val > ans[nh][nw][dist]) continue;
    vector<int> dh = {0,1,0,-1};
    vector<int> dw = {1,0,-1,0};
    for (int i=0;i<4;i++) {
      int toh = nh+dh[i];
      int tow = nw+dw[i];
      if (toh < 0 || toh >= h || tow < 0 || tow >= w) continue;
      int kans = val+(2*dist+1)*a[toh][tow];
      if (ans[toh][tow][dist+1] > kans) {
        ans[toh][tow][dist+1] = kans;
        pq.push(make_tuple(kans,toh,tow,dist+1));
      }
    }
  }
  int tans = INF;
  for (int i=0;i<=h*w;i++) {
    tans = min(tans,ans[h-1][w-1][i]);
  }
  cout << tans << endl;
}
accuracy: 1
time_ms: 60
memory_kb: 12016
final_rank: 7

code_snippet: #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
using ll = long long;

#define rep(i, s, n) for (int i = (int)(s); i < (int)(n); i++)

int h,w;
ll ans = 1000000000;

int dfs(int h1,int w1,vector<vector<ll>>& m, vector<vector<ll>>& s,ll d,ll t){
    if (h1==h-1 && w1==w-1){
        ans = min(ans,t-d);
        return 0;
    }
    if (h1-1>=0 && s[h1-1][w1]>=t+(2*d+1)*m[h1-1][w1]+1){
        s[h1-1][w1] = t+(2*d+1)*m[h1-1][w1]+1;
        dfs(h1-1,w1,m,s, d+1 , t+(2*d+1)*m[h1-1][w1]+1 );
    }
    if (h1+1<h && s[h1+1][w1]>=t+(2*d+1)*m[h1+1][w1]+1){
        s[h1+1][w1] = t+(2*d+1)*m[h1+1][w1]+1;
        dfs(h1+1,w1,m,s, d+1 , t+(2*d+1)*m[h1+1][w1]+1 );
    }
    if (w1+1<w && s[h1][w1+1] >= t+(2*d+1)*m[h1][w1+1]+1){
        s[h1][w1+1] = t+(2*d+1)*m[h1][w1+1]+1;
        dfs(h1,w1+1,m,s, d+1 , t+(2*d+1)*m[h1][w1+1]+1 );
    }
    if (w1-1>=0 && s[h1][w1-1] >= t+(2*d+1)*m[h1][w1-1]+1){
        s[h1][w1-1] = t+(2*d+1)*m[h1][w1-1]+1;
        dfs(h1,w1-1,m,s, d+1 , t+(2*d+1)*m[h1][w1-1]+1 );
    }
    return 0;
}

int main(){
    cin>>h>>w;
    vector<vector<ll>> m(h,vector<ll>(w,0)),s(h,vector<ll>(w,10000000000));
    rep(i,0,h){
        rep(j,0,w){
            cin>>m[i][j];
        }
    }
    s[0][0] = 0;
    dfs(0,0,m,s,0,0);
    cout<<ans<<endl;
}
accuracy: 0.6136363636363636
time_ms: 420
memory_kb: 3592
final_rank: 20

code_snippet: #include <iostream>
#include <vector>
#include <utility>
#include <tuple>
#include <queue>
using namespace std;

int main(){
  int h, w;
  cin >> h >> w;
  vector<vector<long long>> A(h,vector<long long>(w));
  for(int i = 0; i < h; ++i){
    for(int j = 0; j < w; ++j){
      cin >> A[i][j];
    }
  }
  const long long INF = 1e18;
  vector dp(h,vector(w,vector(h*w,INF)));
  dp[0][0][0] = 0;
  const int dx[] = {1,0,-1,0}, dy[] = {0,1,0,-1};
  using tup = tuple<long long,int,int,int>;
  priority_queue<tup,vector<tup>,greater<>> pq;
  pq.emplace(0,0,0,0);// time, x, y, dist
  while(pq.size()){
    auto [t, i, j, k] = pq.top();
    pq.pop();
    if(dp[i][j][k] < t) continue;
    
    for(int l = 0; l < 4; ++l){
      int i_ = i + dx[l];
      int j_ = j + dy[l];
      int k_ = k + 1;
      if(i_ < 0 or i_ >= h) continue;
      if(j_ < 0 or j_ >= w) continue;
      if(k_ >= h*w) continue;
      long long t_ = min(dp[i_][j_][k_],
                         dp[i][j][k]+A[i_][j_]*(2*k+1)+1);
      if(t_ < dp[i_][j_][k_]){
        pq.emplace(t_,i_,j_,k_);
        dp[i_][j_][k_] = t_;
      }
    }
  }

  long long ans = INF, a = A[h-1][w-1];
  for(int i = 0; i < h*w; ++i){
    ans = min(ans,dp[h-1][w-1][i]-i);
  }
  cout << ans << endl;
}
accuracy: 1
time_ms: 60
memory_kb: 16280
final_rank: 10

code_snippet: #include<bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int,int>;
constexpr ll INF = 1e18;

using tup = tuple<ll,int,int,int>;

ll dp[910][35][35];

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    for(int i=0; i<910; i++){
        for(int j=0; j<35; j++){
            for(int k=0; k<35; k++){
                dp[i][j][k] = INF;
            }
        }
    }
    int H,W;
    cin >> H >> W;
    vector<vector<int>> G(H,vector<int>(W));
    for(int i=0; i<H; i++){
        for(int j=0; j<W; j++){
            cin >> G[i][j];
        }
    }
    vector<int> dx = {0,-1,0,1};
    vector<int> dy = {-1,0,1,0};
    priority_queue<tup,vector<tup>,greater<>> que;
    que.emplace(0,0,0,0);
    dp[0][0][0] = 0;
    while(que.size()){
        ll cost;
        int mn,y,x;
        tie(cost,mn,y,x) = que.top();
        que.pop();
        if(mn == 900) continue;
        if(dp[mn][y][x] < cost) continue;
        for(int k=0; k<4; k++){
            int ny = y + dy[k];
            int nx = x + dx[k];
            if(ny<0 || ny>=H || nx<0 || nx>=W) continue;
            ll ncost = (ll)G[ny][nx] * mn * 2 + G[ny][nx];
            if(dp[mn+1][ny][nx] > cost + ncost){
                dp[mn+1][ny][nx] = cost + ncost;
                que.emplace(dp[mn+1][ny][nx],mn+1,ny,nx);
            }
        }
    }
    ll ans = INF;
    for(int i=0; i<910; i++){
        if(ans > dp[i][H-1][W-1]){
            ans = dp[i][H-1][W-1];
        }
    }
    cout << ans << endl;
    return 0;
}
accuracy: 1
time_ms: 50
memory_kb: 19896
final_rank: 15

code_snippet: #include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#include <queue>
#include <cstdio>
#include <ctime>
#include <assert.h>
#include <chrono>
#include <random>
#include <numeric>
#include <set>
using namespace std;
typedef long long int ll;
using ull = unsigned long long;

int dp[33][33][901];
int a[33][33];

int dx[]={1,-1,0,0};
int dy[]={0,0,1,-1};

int main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int h,w; cin >> h >> w;
    for(int i=0;i<h;i++){
        for(int j=0;j<w;j++){
            cin >> a[i][j];
            for(int k=0;k<901;k++){
                dp[i][j][k]=1e9;
            }
        }
    }
    dp[0][0][0]=0;
    for(int d=0;d<h*w;d++){
        for(int i=0;i<h;i++){
            for(int j=0;j<w;j++){
                for(int k=0;k<4;k++){
                    int nx=i+dx[k],ny=j+dy[k];
                    if(0<=nx&&nx<h&&0<=ny&&ny<w){
                        dp[nx][ny][d+1]=min(dp[nx][ny][d+1],dp[i][j][d]+a[nx][ny]*(2*d+1));
                    }
                }
            }
        }
    }
    int res=1e9;
    for(int i=0;i<=h*w;i++){
        res=min(res,dp[h-1][w-1][i]);
    }
    cout << res << endl;
}
accuracy: 1
time_ms: 10
memory_kb: 6424
final_rank: 1

code_snippet: // #pragma GCC target("avx")  // CPU 処理並列化
// #pragma GCC optimize("O3")  // CPU 処理並列化
// #pragma GCC optimize("unroll-loops")  // 条件処理の呼び出しを減らす
// #define BEGIN_STACK_EXTEND(size) void * stack_extend_memory_ = malloc(size);void * stack_extend_origin_memory_;char * stack_extend_dummy_memory_ = (char*)alloca((1+(int)(((long long)stack_extend_memory_)&127))*16);*stack_extend_dummy_memory_ = 0;asm volatile("mov %%rsp, %%rbx\nmov %%rax, %%rsp":"=b"(stack_extend_origin_memory_):"a"((char*)stack_extend_memory_+(size)-1024));
// #define END_STACK_EXTEND asm volatile("mov %%rax, %%rsp"::"a"(stack_extend_origin_memory_));free(stack_extend_memory_);

#include<stdio.h>
#include<math.h>
#include<algorithm>
#include<queue>
#include<deque>
#include<stack>
#include<string>
#include<string.h>
#include<vector>
#include<set>
#include<map>
#include<bitset>
#include<stdlib.h>
#include<cassert>
#include<time.h>
#include<bitset>
#include<numeric>
#include<unordered_set>
#include<unordered_map>
#include<complex>
using namespace std;
const long long mod=1000000007;
const long long inf=mod*mod;
const long long d2=(mod+1)/2;
const long double EPS=1e-9;
const double INF=1e+10;
const double PI=acos(-1.0);
const int C_SIZE = 3100000;
const int UF_SIZE = 3100000;
namespace{
	long long fact[C_SIZE];
	long long finv[C_SIZE];
	long long inv[C_SIZE];
	long long Comb(int a,int b){
	 	if(a<b||b<0)return 0;
	 	return fact[a]*finv[b]%mod*finv[a-b]%mod;
	}
	void init_C(int n){
		fact[0]=finv[0]=inv[1]=1;
		for(int i=2;i<n;i++){
			inv[i]=(mod-(mod/i)*inv[mod%i]%mod)%mod;
		}
		for(int i=1;i<n;i++){
			fact[i]=fact[i-1]*i%mod;
			finv[i]=finv[i-1]*inv[i]%mod;
		}
	}
	long long pw(long long a,long long b){
		if(a<0LL)return 0;
		if(b<0LL)return 0;
		long long ret=1;
		while(b){
			if(b%2)ret=ret*a%mod;
			a=a*a%mod;
			b/=2;
		}
		return ret;
	}
	long long pw_mod(long long a,long long b,long long M){
		if(a<0LL)return 0;
		if(b<0LL)return 0;
		long long ret=1;
		while(b){
			if(b%2)ret=ret*a%M;
			a=a*a%M;
			b/=2;
		}
		return ret;
	}
	int pw_mod_int(int a,int b,int M){
		if(a<0)return 0;
		if(b<0)return 0;
		int ret=1;
		while(b){
			if(b%2)ret=(long long)ret*a%M;
			a=(long long)a*a%M;
			b/=2;
		}
		return ret;
	}
	int ABS(int a){return max(a,-a);}
	long long ABS(long long a){return max(a,-a);}
	double ABS(double a){return max(a,-a);}
	int sig(double r) { return (r < -EPS) ? -1 : (r > +EPS) ? +1 : 0; }
	int UF[UF_SIZE];
	void init_UF(int n){
		for(int i=0;i<n;i++)UF[i]=-1;
	}
	int FIND(int a){
		if(UF[a]<0)return a;
		return UF[a]=FIND(UF[a]);
	}
	void UNION(int a,int b){
		a=FIND(a);b=FIND(b);if(a==b)return;
		if(UF[a]>UF[b])swap(a,b);
		UF[a]+=UF[b];UF[b]=a;
	}
}
// ここから編集しろ
int p[40][40];
int ijk[40][40][1600];
int v[40][40][1600];
int dx[]={1,0,-1,0};
int dy[]={0,1,0,-1};
int main(){
	int a,b;scanf("%d%d",&a,&b);
	for(int i=0;i<a;i++){
		for(int j=0;j<b;j++)scanf("%d",&p[i][j]);
	}
	for(int i=0;i<a;i++)for(int j=0;j<b;j++)for(int k=0;k<1600;k++)
		ijk[i][j][k]=mod;
	ijk[0][0][0]=0;
	priority_queue<pair<pair<int,int>,pair<int,int> > >Q;
	Q.push(make_pair(make_pair(0,0),make_pair(0,0)));
	while(Q.size()){
		int cost=-Q.top().first.first;
		int K=Q.top().first.second;
		int row=Q.top().second.first;
		int col=Q.top().second.second;
		Q.pop();
		if(v[row][col][K])continue;
		v[row][col][K]=1;
		// printf("%d %d %d: %d\n",row,col,K,cost);
		for(int i=0;i<4;i++){
			int tr=row+dx[i];
			int tc=col+dy[i];
			if(tr<0||tc<0||tr>=a||tc>=b)continue;
			if(K+1>=1500)continue;
			int toc=cost+p[tr][tc]*(K*2+1);
			if(v[tr][tc][K+1])continue;
			if(ijk[tr][tc][K+1]>toc){
				ijk[tr][tc][K+1]=toc;
				Q.push(make_pair(make_pair(-toc,K+1),make_pair(tr,tc)));
			}
		}
	}
	int ret=mod;
	for(int i=0;i<1600;i++)ret=min(ret,ijk[a-1][b-1][i]);
	printf("%d\n",ret);
}
accuracy: 1
time_ms: 120
memory_kb: 20372
final_rank: 17

code_snippet: #include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <tuple>

const std::vector<int> dx{-1, 0, 1, 0}, dy{0, -1, 0, 1};

template <class T>
using MinHeap = std::priority_queue<T, std::vector<T>, std::greater<T>>;

template <class T>
std::vector<T> vec(int len, T elem) { return std::vector<T>(len, elem); }

constexpr int INF = 1 << 30;

void solve() {
    int h, w;
    std::cin >> h >> w;

    auto xss = vec(h, vec(w, 0));
    for (auto& xs : xss) {
        for (auto& x : xs) {
            std::cin >> x;
        }
    }

    auto dp = vec(h, vec(w, vec(h * w, INF)));
    dp[0][0][0] = 0;

    MinHeap<std::tuple<int, int, int, int>> que;
    que.emplace(dp[0][0][0], 0, 0, 0);

    while (!que.empty()) {
        int d, i, j, l;
        std::tie(d, i, j, l) = que.top();
        que.pop();
        if (d > dp[i][j][l]) continue;

        for (int k = 0; k < 4; ++k) {
            int ni = i + dx[k],
                nj = j + dy[k],
                nl = l + 1;
            if (ni < 0 || h <= ni || nj < 0 || w <= nj || nl >= h * w) continue;

            int nd = d + xss[ni][nj] * (l * 2 + 1);
            if (nd >= dp[ni][nj][nl]) continue;

            dp[ni][nj][nl] = nd;
            que.emplace(nd, ni, nj, nl);
        }
    }

    int ans = *std::min_element(dp[h - 1][w - 1].begin(), dp[h - 1][w - 1].end());
    std::cout << ans << std::endl;
}

int main() {
    std::cin.tie(nullptr);
    std::cout.tie(nullptr);
    std::ios::sync_with_stdio(false);

    solve();

    return 0;
}
accuracy: 1
time_ms: 60
memory_kb: 10264
final_rank: 6

code_snippet: #include "iostream"
#include "climits"
#include "list"
#include "queue"
#include "stack"
#include "set"
#include "functional"
#include "algorithm"
#include "string"
#include "map"
#include "unordered_map"
#include "unordered_set"
#include "iomanip"
#include "cmath"
#include "random"
#include "bitset"
#include "cstdio"
#include "numeric"
#include "cassert"
#include "functional"

using namespace std;

constexpr long long int MOD = 1000000007;
//const int MOD = 1000000007;
//constexpr int MOD = 998244353;
//constexpr long long int MOD = 998244353;

constexpr double EPS = 1e-8;

//int N, M, K, H, W, L, R;
long long int N, M, K, H, W, L, R;

struct Node {
	int y, x, dis, cost;
	Node(const int yy, const int xx, const int d, const int c) {
		y = yy, x = xx, dis = d, cost = c;
		return;
	}
	bool operator<(const Node&n)const {
		return cost > n.cost;
	}
};

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);

	cin >> H >> W;
	vector<vector<int>>v(H, vector<int>(W));
	for (auto &i : v)for (auto &j : i)cin >> j;
	vector<vector<vector<int>>>dp(H, vector<vector<int>>(W, vector<int>(H*W, MOD)));
	dp[0][0][0] = 0;
	priority_queue<Node>PQ;
	PQ.push(Node(0, 0, 0, 0));
	int dir[] = { 1,0,-1,0,1 };
	while (!PQ.empty()) {
		auto cn = PQ.top();
		//cout << cn.y << " " << cn.x << " " << cn.dis << " " << cn.cost << endl;
		PQ.pop();
		if (cn.dis == H * W - 1)continue;
		for (int i = 0; i < 4; i++) {
			int ny = cn.y + dir[i];
			int nx = cn.x + dir[i + 1];
			if (ny < 0 || nx < 0 || ny >= H || nx >= W)continue;
			if (dp[ny][nx][cn.dis + 1] > cn.cost + v[ny][nx] * ((cn.dis + 0) * 2 + 1)) {
				dp[ny][nx][cn.dis + 1] = cn.cost + v[ny][nx] * ((cn.dis + 0) * 2 + 1);
				PQ.push(Node(ny, nx, cn.dis + 1, dp[ny][nx][cn.dis + 1]));
			}
		}
	}
	int ans = MOD;
	for (int i = 0; i < H*W; i++)ans = min(ans, dp.back().back()[i]);
	cout << ans << endl;
}
accuracy: 1
time_ms: 50
memory_kb: 10132
final_rank: 5

code_snippet: #include "bits/stdc++.h"
#pragma GCC optimize("Ofast")

// Begin {{{
using namespace std;

#ifndef DEBUG
#define dump(...)
#endif

template <class A, class B> inline bool chmax(A &a, const B &b) { return b > a && (a = b, true); }
template <class A, class B> inline bool chmin(A &a, const B &b) { return b < a && (a = b, true); }

template <class T>
inline vector<T> make_v(const T &initvalue, size_t sz) {
    return vector<T>(sz, initvalue);
}

template <class T, class... Args>
inline auto make_v(const T &initvalue, size_t sz, Args... args) {
    return vector<decltype(make_v<T>(initvalue, args...))>(sz, make_v<T>(initvalue, args...));
}

constexpr int INF = 0x3f3f3f3f;
constexpr int64_t LINF = 0x3f3f3f3f3f3f3f3fLL;

constexpr size_t    operator""_sz(unsigned long long value) { return value; };
constexpr intmax_t  operator""_im(unsigned long long value) { return value; };
constexpr uintmax_t operator""_um(unsigned long long value) { return value; };
// }}} End

constexpr int dy[] = {0, 1, 0, -1, -1, 1, 1, -1};
constexpr int dx[] = {1, 0, -1, 0, 1, 1, -1, -1};

inline bool inside(int y, int x, int H, int W) {
    return (y >= 0 && x >= 0 && y < H && x < W);
}

size_t H, W;
auto a = make_v(0_im, 35, 35);

auto dp = make_v(-1_im, 35, 35, 1225);

intmax_t rec(int y, int x, intmax_t dist) {
    if (dist < 0) return INF;
    if (y == 0 && x == 0) return 0;
    if (~dp[y][x][dist]) return dp[y][x][dist];

    intmax_t ret = INF;
    for (size_t i = 0; i < 4; ++i) {
        int ny = y + dy[i];
        int nx = x + dx[i];
        if (inside(ny, nx, H, W)) {
            chmin(ret, rec(ny, nx, dist - 1) + ((dist - 1) * 2 + 1) * a[y][x]);
        }
    }

    return dp[y][x][dist] = ret;
}

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

    cin >> H >> W;

    for (size_t i = 0; i < H; ++i) {
        for (size_t j = 0; j < W; ++j) {
            cin >> a[i][j];
        }
    }

    intmax_t res = INF;
    for (intmax_t d = 1; d <= H * W; ++d) {
        chmin(res, rec(H - 1, W - 1, d));
    }

    cout << res << "\n";

    return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 14972
final_rank: 8