import std.bigint,std.container,std.array,std.algorithm,std.conv,
std.functional,std.math,std.numeric,std.string,std.range,std.complex,
std.typecons,std.regex,std.random,std.uni,std.traits,std.variant;
import core.stdc.stdio : freopen;
import core.stdc.stdlib : system;
import std.stdio;
//import core.stdc.stdio;
alias RedBlackTree Rbt;
alias redBlackTree rbt;
alias BigInt big;
alias pair(X,Y)=Tuple!(X,"fi",Y,"se");
alias pair!(int,int) pii;
alias pair!(long,long) pll;
alias BoyerMooreFinder strfind;
alias make_pair mp;
alias binary_search bins;
alias gcd=std.numeric.gcd;
immutable int mod=1000000007;
pure nothrow
{
	T lcm (T)(auto ref T a,auto ref T b)
	{
		return a/gcd(a,b)*b;
	}
	pair!(X,Y) make_pair(X,Y)(in X x_,in Y y_)
	{
		pair!(X,Y) pp;
		pp.fi=x_;
		pp.se=y_;
		return pp;
	}
	big gcd(big a,big b)
	{
		while(b)
		{
			a%=b;
			swap(a,b);
		}
		return a;
	}
	X binpow(X,Y)(X base,Y exp)if(is(typeof(exp>>1)))
	{
		if(exp<0)return X(0);
		X res=X(1);
		while(exp>0)
		{
			if(exp&1)res*=base;
			base*=base;
			exp>>=1;
		}
		return res;
	}
	auto binpow(X,Y,Z)(X base,Y exp,in Z mm)if(is(typeof(exp>>1)) && is(typeof(base%mm)))
	{
		if(mm==0) return binpow(base,exp);
		if(exp<0)return X(0);
		auto res=X(1)%mm;
		while(exp>0)
		{
			if(exp&1)res=(res*base)%mm;
			base*=base;
			base%=mm;
			exp>>=1;
		}
		return res;
	}
	@property X sqr(X)(in X a_) {return a_*a_;}
	@property X cub(X)(in X a_) {return a_*a_*a_;}
	size_t lowb(T,X)(in T a,auto ref X g) if(isRandomAccessRange!T && hasLength!T && is(typeof(g<a[0])==bool))
	{
		size_t l=0,r=a.length;
		while(r-l>1)
		{
			auto m=(l+r)>>1;
			if(!(a[m]<g))r=m;
			else l=m;
		}
		return (!(a[l]<g))?l:r;
	}
	size_t upb(T,X)(in T a,auto ref X g)if(isRandomAccessRange!T && hasLength!T && is(typeof(g<a[0])==bool))
	{
		auto pos=lowb(a,g);
		if(pos==a.length)return pos;
		else return a[pos]==g?pos+1:pos;
	}
	size_t binf(T,X)(in T a,auto ref X g) if(isRandomAccessRange!T && hasLength!T && is(typeof(g<a[0])==bool))
	{
		auto pos=lowb(a,g);
		return (g==a[pos])?pos:a.length;
	}
	bool binary_search(T,X)(in T a,auto ref X g)if(isRandomAccessRange!T && hasLength!T && is(typeof(g<a[0])==bool))
	{
		return binf(a,g)!=a.length;
	}
}
void putarr(X)(in X a,in char ch=' '){foreach(const ref i;a)write(i,ch);}
void putarr(X)(in X a,in size_t n=a.length,in size_t m=a.front.length)
{
	foreach(i;0..n)
	{
		foreach(j;0..m)write(a[i][j],' ');
		writeln;
	}
}
bool getarr(X)(X a,in size_t n)
{bool b=1; foreach(ref i;a[0..n])b=input(&i);return b;}
bool input(T...)(T ptrs) if (ptrs.length > 0)
{return readf(replicate(" %s", ptrs.length), ptrs)==ptrs.length;}
bool read(T...)(T ptrs) if (ptrs.length > 0)
{return readf(replicate(" %s", ptrs.length)~"\n", ptrs)==ptrs.length;}
nothrow auto inf(in char* name) {return freopen(name,"r",core.stdc.stdio.stdin);}
nothrow auto ouf(in char* name) {return freopen(name,"w",core.stdc.stdio.stdout);}
@property auto arread(T)(){return readln.split.map!(to!(T)).array;}
//split strip isAlpha isNumber isWhite isLower isUpper toLower toUpper mixin
//cumulativeFold schwartzSort multiSort partialSort heapify join filter
//---------------------------------------------------------------program beginning-----------------------------------------------------------------

void main()
{
	int n,k;
	loop:while(read(&n,&k))
	{
		auto a=arread!(int);
		auto b=iota(1,n+1).array;
		int p=0;
		foreach(i;0..k)
		{
			int pos=(p+a[i])%b.length;
			//debug writeln(a.length);
			write(b[pos],' ');
			if(pos==0)b=b[pos+1..$];
			else if(pos==b.length-1)b=b[0..pos];
			else b=b[0..pos]~b[pos+1..$];
			p=pos%b.length;
		}
	}
	debug system("pause");
}

import std.algorithm;
import std.array;
import std.conv;
import std.range;
import std.stdio;
import std.string;

void main ()
{
	int n, k;
	while (readf (" %s %s", &n, &k) > 0)
	{
		readln;
		auto a = readln.split.map !(to !(int)).array;
		auto p = iota (1, n + 1).array;
		int [] ans;
		foreach (v; a)
		{
			int q = v % p.length;
			ans ~= p[q];
			p = p[q + 1..$] ~ p[0..q];
		}
		writefln ("%(%s %)", ans);
	}
}


void main() {
  import std.stdio, std.string, std.conv, std.algorithm;

  int n;
  rd(n);
  auto s = readln.chomp.to!(char[]);
  auto a = readln.split.to!(long[]);

  const char[] pat = "hard";
  auto dp = new long[](pat.length + 1);
  fill(dp, 1_000_000_000_000_000_000);
  dp[0] = 0;
  foreach (i; 0 .. n) {
    auto nex = new long[](pat.length + 1);
    fill(nex, 1_000_000_000_000_000_000);
    foreach (j, c; pat) {
      if (s[i] == c) {
        nex[j] = min(nex[j], dp[j] + a[i]);
        nex[j + 1] = min(nex[j + 1], dp[j]);
      } else {
        nex[j] = min(nex[j], dp[j]);
      }
    }
    dp.swap(nex);
  }
  writeln(reduce!(min)(dp[0 .. (pat.length)]));
}

void rd(T...)(ref T x) {
  import std.stdio : readln;
  import std.string : split;
  import std.conv : to;

  auto l = readln.split;
  assert(l.length == x.length);
  foreach (i, ref e; x)
    e = l[i].to!(typeof(e));
}

import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, core.stdc.string;

void main() {
    auto N = readln.chomp.to!int;
    auto S = readln.chomp;
    auto A = readln.split.map!(to!long).array;

    auto dp = new long[][](N+1, 4);
    foreach (i; 0..N+1) fill(dp[i], 1L<<59);
    dp[0][0] = 0;

    foreach (i; 0..N) {
        dp[i+1] = dp[i].dup;
        if (S[i] == 'h') {
            dp[i+1][0] = dp[i][0] + A[i];
            dp[i+1][1] = min(dp[i][0], dp[i][1]);
        } else if (S[i] == 'a') {
            dp[i+1][1] = dp[i][1] + A[i];
            dp[i+1][2] = min(dp[i][1], dp[i][2]);
        } else if (S[i] == 'r') {
            dp[i+1][2] = dp[i][2] + A[i];
            dp[i+1][3] = min(dp[i][2], dp[i][3]);
        } else if (S[i] == 'd') {
            dp[i+1][3] = dp[i][3] + A[i];
        }
    }

    dp[N].reduce!min.writeln;
}

import core.bitop, std.bitmanip;
import std.algorithm, std.functional;
import std.array, std.container;
import std.conv;
import std.math, std.numeric;
import std.range, std.range.interfaces;
import std.stdio, std.string;
import std.typecons;

immutable int INF = 10L ^^ 9 + 23;

void main()
{
    int n;
    readf("%s", &n);
    readln;

    auto s = "#" ~ readln.chomp;

    auto c = [0] ~ readln.chomp.split.map!(to!int).array;

    auto dp = new long[][] (n+1, 5);
    foreach (ref rw; dp) { rw.fill(0); }

    int [char] mp;
    mp['h'] = 1;
    mp['a'] = 2;
    mp['r'] = 3;
    mp['d'] = 4;
    foreach (i; 1 .. n+1) {
        dp[i][] = dp[i-1][];

        int k = mp.get(s[i], -1);
        if (k == -1) { continue; }

        dp[i][k-1] = min(dp[i-1][k-1] + c[i], k-1 > 0 ? dp[i-1][0 .. k-1].minElement : 10L ^^ 18);
        dp[i][k] = min(dp[i-1][k], dp[i-1][k-1]);
    }

    debug { dp.writeln; }

    dp[n][0 .. 4].minElement.writeln;
}

import core.bitop, std.bitmanip;
import std.algorithm, std.functional;
import std.array, std.container;
import std.conv;
import std.math, std.numeric;
import std.range, std.range.interfaces;
import std.stdio, std.string;
import std.typecons;

immutable int INF = 10L ^^ 9 + 23;

void main()
{
    int n;
    readf("%s", &n);
    readln;

    auto s = "#" ~ readln.chomp;

    auto c = [0] ~ readln.chomp.split.map!(to!int).array;

    auto dp = new long[][] (n+1, 5);
    foreach (ref rw; dp) { rw.fill(0); }

    int [char] mp;
    mp['h'] = 1;
    mp['a'] = 2;
    mp['r'] = 3;
    mp['d'] = 4;
    foreach (i; 1 .. n+1) {
        dp[i][] = dp[i-1][];

        if (s[i] ! in mp) { continue; }
        int k = mp[s[i]];

        dp[i][k-1] = min(dp[i-1][k-1] + c[i], k-2 >= 0 ? dp[i-1][k-2] : 10L ^^ 18);
        dp[i][k] = min(dp[i-1][k], dp[i-1][k-1]);

        debug { dp[i][k].writeln; }
    }

    debug { dp.writeln; }

    dp[n][0 .. 4].minElement.writeln;
}

import std.stdio, std.string, std.conv;
import std.range, std.algorithm, std.array, std.typecons, std.container;
import std.math, std.numeric, core.bitop;

enum inf3 = 1_001_001_001;
enum inf6 = 1_001_001_001_001_001_001L;
enum mod = 1_000_000_007L;

void main() {
    int n;
    scan(n);
    auto s = readln.chomp;
    auto a = readln.split.to!(long[]);

    auto t = "hard";
    auto m = t.length.to!int;

    auto dp = new long[][](n + 1, m + 1);
    fillAll(dp, inf6);
    dp[0][0] = 0;

    foreach (i ; 0 .. n) {
        foreach (j ; 0 .. m + 1) {
            if (j < m && s[i] == t[j]) {
                dp[i+1][j+1] = min(dp[i+1][j+1], dp[i][j]);
                dp[i+1][j] = min(dp[i+1][j], dp[i][j] + a[i]);
            }
            else {
                dp[i+1][j] = min(dp[i+1][j], dp[i][j]);
            }
        }
    }

    debug {
        writefln("%(%s\n%)", dp);
    }

    long ans = inf6;

    foreach (j ; 0 .. m) {
        ans = min(ans, dp[n][j]);
    }

    writeln(ans);
}

void scan(T...)(ref T args) {
    import std.stdio : readln;
    import std.algorithm : splitter;
    import std.conv : to;
    import std.range.primitives;

    auto line = readln().splitter();
    foreach (ref arg; args) {
        arg = line.front.to!(typeof(arg));
        line.popFront();
    }
    assert(line.empty);
}

void fillAll(R, T)(ref R arr, T value) {
    static if (is(typeof(arr[] = value))) {
        arr[] = value;
    }
    else {
        foreach (ref e; arr) {
            fillAll(e, value);
        }
    }
}

void main() {
  import std.stdio, std.string, std.conv, std.algorithm;

  int n;
  rd(n);
  auto s = readln.chomp.to!(char[]);
  auto a = readln.split.to!(long[]);

  const char[] pat = "hard";
  auto dp = new long[](pat.length + 1);
  fill(dp, 1_000_000_000_000_000_000);
  dp[0] = 0;
  foreach (i; 0 .. n) {
    auto nex = new long[](pat.length + 1);
    fill(nex, 1_000_000_000_000_000_000);
    foreach (j, c; pat) {
      if (s[i] == c) {
        nex[j] = min(nex[j], dp[j] + a[i]); // s[i]を消す
        nex[j + 1] = min(nex[j + 1], dp[j]); // 消さない
      } else {
        nex[j] = min(nex[j], dp[j]); // 消さない
      }
    }
    dp.swap(nex);
  }
  writeln(reduce!(min)(dp[0 .. (pat.length)]));
}

void rd(T...)(ref T x) {
  import std.stdio : readln;
  import std.string : split;
  import std.conv : to;

  auto l = readln.split;
  assert(l.length == x.length);
  foreach (i, ref e; x)
    e = l[i].to!(typeof(e));
}

void main() {
  import std.stdio, std.string, std.conv, std.algorithm;

  int n;
  rd(n);
  auto s = readln.chomp.to!(char[]);
  auto a = readln.split.to!(long[]);

  const char[] pat = "hard";
  auto dp = new long[][](n + 1, pat.length + 1);
  foreach (i; 0 .. (n + 1))
    fill(dp[i], 1_000_000_000_000_000_000);
  dp[0][0] = 0;
  foreach (i; 0 .. n) {
    foreach (j, c; pat) {
      if (s[i] == c) {
        dp[i + 1][j] = min(dp[i + 1][j], dp[i][j] + a[i]);
        dp[i + 1][j + 1] = min(dp[i + 1][j + 1], dp[i][j]);
      } else {
        dp[i + 1][j] = min(dp[i + 1][j], dp[i][j]);
      }
    }
  }
  writeln(reduce!(min)(dp[n][0 .. (pat.length)]));
}

void rd(T...)(ref T x) {
  import std.stdio : readln;
  import std.string : split;
  import std.conv : to;

  auto l = readln.split;
  assert(l.length == x.length);
  foreach (i, ref e; x)
    e = l[i].to!(typeof(e));
}

import core.bitop, std.bitmanip;
import std.algorithm, std.functional;
import std.array, std.container;
import std.conv;
import std.math, std.numeric;
import std.range, std.range.interfaces;
import std.stdio, std.string;
import std.typecons;

immutable int INF = 10L ^^ 9 + 23;

void main()
{
    int n;
    readf("%s", &n);
    readln;

    auto s = "#" ~ readln.chomp;

    auto c = [0] ~ readln.chomp.split.map!(to!int).array;

    auto dp = new long[][] (n+1, 5);
    foreach (ref rw; dp) { rw.fill(0); }

    int [char] mp;
    mp['h'] = 1;
    mp['a'] = 2;
    mp['r'] = 3;
    mp['d'] = 4;
    foreach (i; 1 .. n+1) {
        dp[i][] = dp[i-1][];

        if (s[i] ! in mp) { continue; }
        int k = mp[s[i]];

        dp[i][k-1] = min(dp[i-1][k-1] + c[i], k-1 > 0 ? dp[i-1][0 .. k-1].minElement : 10L ^^ 18);
        dp[i][k] = max(dp[i-1][k], dp[i-1][k-1]);
    }

    debug { dp.writeln; }

    dp[n][0 .. 4].minElement.writeln;
}

void main() {
  import std.stdio, std.string, std.conv, std.algorithm;

  int n;
  rd(n);
  auto s = readln.chomp.to!(char[]);
  auto a = readln.split.to!(long[]);

  const char[] pat = "hard";
  auto dp = new long[][](n + 1, pat.length + 1);
  foreach (i; 0 .. (n + 1))
    fill(dp[i], 1_000_000_000_000_000_000);
  dp[0][0] = 0;
  foreach (i; 0 .. n) {
    foreach (j, c; pat) {
      if (s[i] == c) {
        dp[i + 1][j] = min(dp[i + 1][j], dp[i][j] + a[i]);
        dp[i + 1][j + 1] = min(dp[i + 1][j + 1], dp[i][j]);
      } else {
        dp[i + 1][j] = min(dp[i + 1][j], dp[i][j]);
      }
    }
  }
  writeln(reduce!(min)(dp[n]));
}

void rd(T...)(ref T x) {
  import std.stdio : readln;
  import std.string : split;
  import std.conv : to;

  auto l = readln.split;
  assert(l.length == x.length);
  foreach (i, ref e; x)
    e = l[i].to!(typeof(e));
}


import core.bitop, std.bitmanip;
import core.checkedint;
import std.algorithm, std.functional, std.meta;
import std.array, std.container;
import std.bigint;
import std.conv;
import std.math, std.numeric;
import std.range, std.range.interfaces;
import std.stdio, std.string;
import std.typecons;

void main()
{
    int n, m, k;
    readf("%s %s %s", &n, &m, &k);
    readln;

    string[] arr;
    foreach (_; 0 .. n) {
        arr ~= readln.chomp;
    }

    int x1, y1, x2, y2;
    readf("%s %s %s %s", &x1, &y1, &x2, &y2);
    readln;

    --x1, --y1;
    --x2, --y2;

    auto step = new int[][] (n, m);
    foreach (rw; 0 .. n) { step[rw][] = -1; }

    auto d = [[0, 1], [-1, 0], [0, -1], [1, 0]];

    alias t = Tuple!(int, int);
    auto q = make!(DList!t);
    step[x1][y1] = 0;
    q ~= tuple(x1, y1);
    while (!q.empty()) {
        auto v = q.front();
        q.removeFront();

        debug { writeln(v[0], ' ', v[1]); }

        foreach (cd; d) {
            int cx = v[0], cy = v[1];
            foreach (_; 0 .. k) {
                cx += cd[0], cy += cd[1];

                if (cx >= n || cx < 0 || cy >= m || cy < 0) { break; }
                if (arr[cx][cy] == '#') { break; }
                if (step[cx][cy] != -1 && step[cx][cy] <= step[v[0]][v[1]]) { break; }

                if (step[cx][cy] == -1 || step[v[0]][v[1]] + 1 < step[cx][cy]) {
                    step[cx][cy] = step[v[0]][v[1]] + 1;
                    q ~= tuple(cx, cy);
                }
            }
        }
    }

    auto ans = step[x2][y2];
    ans.writeln;
}

import core.bitop, std.bitmanip;
import core.checkedint;
import std.algorithm, std.functional, std.meta;
import std.array, std.container;
import std.bigint;
import std.conv;
import std.math, std.numeric;
import std.range, std.range.interfaces;
import std.stdio, std.string;
import std.typecons;

void main()
{
    int n, m, k;
    readf("%s %s %s", &n, &m, &k);
    readln;

    string[] arr;
    foreach (_; 0 .. n) {
        arr ~= readln.chomp;
    }

    int x1, y1, x2, y2;
    readf("%s %s %s %s", &x1, &y1, &x2, &y2);
    readln;

    --x1, --y1;
    --x2, --y2;

    immutable int INF = 10 ^^ 9 + 23;
    auto step = new int[][] (n, m);
    foreach (rw; 0 .. n) { step[rw][] = INF; }

    auto d = [[0, 1], [-1, 0], [0, -1], [1, 0]];

    alias t = Tuple!(int, int);
    auto q = make!(DList!t);
    step[x1][y1] = 0;
    q ~= tuple(x1, y1);
    while (!q.empty()) {
        auto sx = q.front()[0], sy = q.front()[1];
        q.removeFront();

        debug { writeln(sx, ' ', sy); }

        foreach (cd; d) {
            int cx = sx, cy = sy;
            foreach (_; 0 .. k) {
                cx += cd[0], cy += cd[1];

                if (cx >= n || cx < 0 || cy >= m || cy < 0) { break; }
                if (arr[cx][cy] == '#') { break; }
                if (step[cx][cy] <= step[sx][sy]) { break; }

                if (step[sx][sy] + 1 < step[cx][cy]) {
                    step[cx][cy] = step[sx][sy] + 1;
                    q ~= tuple(cx, cy);
                }
            }
        }
    }

    auto ans = step[x2][y2] != INF ? step[x2][y2] : -1;
    ans.writeln;
}

import core.bitop, std.bitmanip;
import core.checkedint;
import std.algorithm, std.functional, std.meta;
import std.array, std.container;
import std.bigint;
import std.conv;
import std.math, std.numeric;
import std.range, std.range.interfaces;
import std.stdio, std.string;
import std.typecons;

void main()
{
    int n, m, k;
    readf("%s %s %s", &n, &m, &k);
    readln;

    string[] arr;
    foreach (_; 0 .. n) {
        arr ~= readln.chomp;
    }

    int x1, y1, x2, y2;
    readf("%s %s %s %s", &x1, &y1, &x2, &y2);
    readln;

    --x1, --y1;
    --x2, --y2;

    auto step = new int[][] (n, m);
    foreach (rw; 0 .. n) { step[rw][] = -1; }

    auto d = [[0, 1], [-1, 0], [0, -1], [1, 0]];

    alias t = Tuple!(int, int);
    auto q = make!(DList!t);
    step[x1][y1] = 0;
    q ~= tuple(x1, y1);
    while (!q.empty()) {
        auto v = q.front();
        q.removeFront();

        debug { writeln(v[0], ' ', v[1]); }

        foreach (cd; d) {
            int cx = v[0], cy = v[1];
            foreach (_; 0 .. k) {
                cx += cd[0], cy += cd[1];

                if (cx >= n || cx < 0 || cy >= m || cy < 0) { break; }
                if (arr[cx][cy] == '#') { break; }
                if (step[cx][cy] != -1 && step[cx][cy] <= step[v[0]][v[1]] + 1) { break; }

                step[cx][cy] = step[v[0]][v[1]] + 1;
                q ~= tuple(cx, cy);
            }
        }
    }

    auto ans = step[x2][y2];
    ans.writeln;
}

import core.bitop, std.bitmanip;
import core.checkedint;
import std.algorithm, std.functional, std.meta;
import std.array, std.container;
import std.bigint;
import std.conv;
import std.math, std.numeric;
import std.range, std.range.interfaces;
import std.stdio, std.string;
import std.typecons;

void main()
{
    int n, m, k;
    readf("%s %s %s", &n, &m, &k);
    readln;

    string[] arr;
    foreach (_; 0 .. n) {
        arr ~= readln.chomp;
    }

    int x1, y1, x2, y2;
    readf("%s %s %s %s", &x1, &y1, &x2, &y2);
    readln;

    --x1, --y1;
    --x2, --y2;

    auto step = new int[][] (n, m);
    foreach (rw; 0 .. n) { step[rw][] = -1; }

    auto d = [[0, 1], [-1, 0], [0, -1], [1, 0]];

    alias t = Tuple!(int, int);
    auto q = make!(DList!t);
    step[x1][y1] = 0;
    q ~= tuple(x1, y1);
    while (!q.empty()) {
        auto v = q.front();
        q.removeFront();

        debug { writeln(v[0], ' ', v[1]); }

        foreach (cd; d) {
            int cx = v[0], cy = v[1];
            foreach (_; 0 .. k) {
                cx += cd[0], cy += cd[1];

                if (cx >= n || cx < 0 || cy >= m || cy < 0) { break; }
                if (arr[cx][cy] == '#') { break; }
                if (step[cx][cy] >= 0) { break; }

                step[cx][cy] = step[v[0]][v[1]] + 1;
                q ~= tuple(cx, cy);
            }
        }
    }

    auto ans = step[x2][y2];
    ans.writeln;
}


import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric, std.container, std.range;

void get(Args...)(ref Args args)
{
    import std.traits, std.meta, std.typecons;

    static if (Args.length == 1) {
        alias Arg = Args[0];

        static if (isArray!Arg) {
          static if (isSomeChar!(ElementType!Arg)) {
            args[0] = readln.chomp.to!Arg;
          } else {
            args[0] = readln.split.to!Arg;
          }
        } else static if (isTuple!Arg) {
            auto input = readln.split;
            static foreach (i; 0..Fields!Arg.length) {
                args[0][i] = input[i].to!(Fields!Arg[i]);
            }
        } else {
            args[0] = readln.chomp.to!Arg;
        }
    } else {
        auto input = readln.split;
        assert(input.length == Args.length);

        static foreach (i; 0..Args.length) {
            args[i] = input[i].to!(Args[i]);
        }
    }
}

void get_lines(Args...)(size_t N, ref Args args)
{
    import std.traits, std.range;

    static foreach (i; 0..Args.length) {
        static assert(isArray!(Args[i]));
        args[i].length = N;
    }

    foreach (i; 0..N) {
        static if (Args.length == 1) {
            get(args[0][i]);
        } else {
            auto input = readln.split;
            static foreach (j; 0..Args.length) {
                args[j][i] = input[j].to!(ElementType!(Args[j]));
            }
        }
    }
}

void main()
{
    int T; get(T);
    while (T--) {
        int N; long K; get(N, K);
        long[] ps; get(ps);
        long r, q = ps[0];
        foreach (p; ps[1..$]) {
            auto a = p * 100;
            auto b = q * K;
            if (a > b) {
                auto c = (a - b + K - 1) / K;
                q += c;
                r += c;
            }
            q += p;
        }
        writeln(r);
    }
}

import std.stdio, std.string, std.algorithm, std.conv, std.range;
void main()
{
    int q;
    readf!"%d\n"(q);
    foreach (_; 0 .. q)
    {
        int n, k;
        readf!"%d %d\n"(n, k);
        long[] a = readln.split.map!(to!long).array;
        long s = a[0], res = 0;
        foreach (i; 1 .. n)
        {
            res = max(res, (100 * a[i] - k * s + k - 1) / k);
            s += a[i];
        }
        writeln(res);
    }
}

import std.algorithm;
import std.conv;
import std.range;
import std.stdio;
import std.string;

void main ()
{
	auto tests = readln.strip.to !(int);
	foreach (test; 0..tests)
	{
		int n, k;
		readf !(" %s %s") (n, k);
		readln;
		auto p = readln.splitter.map !(to !(int)).array;

		bool ok (long start)
		{
			long cur = start + p[0];
			foreach (i; 1..n)
			{
				if (cur * k < p[i] * 100L)
				{
					return false;
				}
				cur += p[i];
			}
			return true;
		}

		long lo = 0;
		long hi = 10L ^^ 12;
		while (lo < hi)
		{
			long me = (lo + hi) >> 1;
			if (ok (me))
			{
				hi = me;
			}
			else
			{
				lo = me + 1;
			}
		}
		writeln (lo);
	}
}

import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.bigint, std.numeric, std.math, std.random;
import core.bitop;

string FMT_F = "%.10f";

static File _f;
void file_io(string fn) { _f = File(fn, "r"); }
static string[] s_rd;
T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }
T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }
T[] RDA(T = long)(T fix = 0) { if (_f.isOpen)  return _RDA!T(_f, fix); else return _RDA!T(fix); }

size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }

void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }

long mod = 10^^9 + 7;
//long mod = 998_244_353;
//long mod = 1_000_003;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
void modpow(ref long x, long y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); }
void modd(ref long x, long y) { y.modpow(mod - 2); x.modm(y); }

void main()
{
	auto t = RD!int;
	auto ans = new long[](t);
	foreach (ti; 0..t)
	{
		auto n = RD!int;
		auto k = RD;
		auto p = RDA;

		auto x = p[0];
		foreach (i; 1..n)
		{
			auto y = p[i] * 100;
			auto d = y - x * k;
			if (d > 0)
			{
				d = (d+k-1) / k;
				ans[ti] += d;
				p[i] += d;
			}
			x += p[i];
		}
	}

	foreach (e; ans)
		writeln(e);
	stdout.flush;
	debug readln;
}

import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.bigint, std.numeric, std.math, std.random;
import core.bitop;

string FMT_F = "%.10f";

static File _f;
void file_io(string fn) { _f = File(fn, "r"); }
static string[] s_rd;
T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }
T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }
T[] RDA(T = long)(T fix = 0) { if (_f.isOpen)  return _RDA!T(_f, fix); else return _RDA!T(fix); }

size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }

void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }

long mod = 10^^9 + 7;
//long mod = 998_244_353;
//long mod = 1_000_003;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
void modpow(ref long x, long y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); }
void modd(ref long x, long y) { y.modpow(mod - 2); x.modm(y); }

void main()
{
	auto t = RD!int;
	auto ans = new long[](t);
	foreach (ti; 0..t)
	{
		auto n = RD!int;
		auto k = RD;
		auto p = RDA;

		auto x = p[0];
		foreach (i; 1..n)
		{
			auto y = p[i] * 100;
			auto d = y - x * k;
			if (d > 0)
			{
				ans[ti] += d;
				p[i] += d;
			}
			x += p[i];
		}
	}

	foreach (e; ans)
		writeln(e);
	stdout.flush;
	debug readln;
}


import std.container;
import std.algorithm;
import std.range;
import std.array;
import std.stdio;
import std.utf;
import std.math;
import std.typecons;

alias type = multi;
struct TestCase
{
  mixin prettyPrinting;

  long n, k;

  void solve(long tc = -1)
  {
    auto divs = redBlackTree!long();
    for(long d = 1; d * d <= n; d++)
      if (n % d == 0)
        {
          divs.insert(d);
          divs.insert(n / d);
        }
    auto bdiv = divs.lowerBound(k + 1).back;
    writeln(n / bdiv);
  }
}

struct Interval
{
  long l, h;

  bool empty()
  {
    return l > h;
  }
}

Interval intersect(Interval a, Interval b)
{
  return Interval(max(a.l, b.l), min(a.h, b.h));
}

struct If
{
  string condition;
}
enum ignore = If("false");
struct Dim
{
  string dimensions;
  int levels;
  this(string dimensions)
  {
    this.dimensions = dimensions;
    levels = cast(int)dimensions.count(",") + 1;
  }
}

template getArrayLevel(W)
{
  static if (isDynamicArray!W && !is(W == string))
    enum getArrayLevel = 1 + getArrayLevel!(removeArray!(W, 1));
  else
    enum getArrayLevel = 0;
}

static foreach(fieldName; 'a' .. cast(char)('z' + 1))
  {
    static if (fieldName != 'o')
      mixin("enum d", fieldName, " = Dim(\"", fieldName, "\");");
  }

mixin template prettyPrinting()
{
  string toString()
  {
    alias structType = typeof(this);
    auto res = appender!(string)();
    res.put(structType.stringof);
    res.put("(");
    static foreach(fieldName; FieldNameTuple!structType)
      {
        res.put(text(" ", fieldName, ": ", mixin(fieldName)));
      }
    res.put(" )");
    res.put("\n");
    return res[];
  }
}

struct Graph(N)
{
  size_t size;
  this(S)(S size)
  {
    adjacencyList.length = cast(size_t) size;
    this.size = cast(size_t) size;
    this.visited = makeSlice!bool(size);
  }
  N[][] adjacencyList;
  alias ne = neighbours;
  N[] neighbours(N n)
  {
    return adjacencyList.at(n);
  }
  alias bcon = biconnect;
  void biconnect(N a, N b)
  {
    adjacencyList.at(a) ~= b;
    adjacencyList.at(b) ~= a;
  }
  alias con = connect;
  void connect(N a, N b)
  {
    adjacencyList.at(a) ~= b;
  }
  void connect(R)(R edges)
  {
    foreach(ref edge; edges)
      {
        connect(edge[0], edge[1]);
      }
  }
  void biconnect(R)(R edges)
  {
    foreach(ref edge; edges)
      {
        biconnect(edge[0], edge[1]);
      }
  }
  alias deg = degree!long;
  auto degree(T)(N a)
  {
    return cast(T) adjacencyList.at(a).length;
  }

  auto visited = new bool[0];
  void dfs(Visitor)(long startNode)
  {
    auto visitor = Visitor.init;
    void internalDfs(long node)
    {
      visited.set(node, true);
      static if (is(typeof({visitor.n = node;})))
        visitor.n = node;
      static if (is(typeof({visitor.pre;})))
        visitor.pre;
      foreach(w; ne(node))
        if (!visited.at(w))
          {
            static if (is(typeof({visitor.w = w;})))
              visitor.w = w;
            static if (is(typeof(visitor.pren)))
              visitor.pren;
            internalDfs(w);
            static if (is(typeof(visitor.postn)))
              visitor.postn;
          }
      static if (is(typeof(visitor.post)))
        visitor.post;
    }
    internalDfs(startNode);
  }
  void dfs(long startNode)
  {
    struct DefVisitor {}
    dfs!DefVisitor(startNode);
  }
}

DList!string _words;

template removeArray(W, int times)
{
  static if (times == 0)
    alias removeArray = W;
  else static if (times == 1)
    alias removeArray = typeof(function(){W w; return w[0];}());
  else
    {
      static assert(isDynamicArray!W);
      alias removeArray = removeArray!(removeArray!(W, 1), times - 1);
    }
}

template baseType(W)
{
  static if (isDynamicArray!W && !is(W == string))
    alias baseType = baseType!(typeof((delegate() {W w; return w[0];})()));
  else
    alias baseType = W;
}

auto makeSlice(E, W, T...)(W size, T otherSizes)
{
  static if (T.length == 0)
    {
      E[] res;
      res.length = cast(size_t) size;
      return res;
    }
  else
    {
      typeof(makeSlice!E(otherSizes))[] res;
      res.length = cast(size_t) size;
      foreach(ref row; res)
        row = makeSlice!E(otherSizes);
      return res;
    }
}

void read(T...)(ref T t)
{
  void basicTypeRead(W)(ref W w)
  {
    import std.stdio: readln;
    while(_words.empty)
      {
        import std.array: split;
        foreach(word; readln.split)
          {
            _words.insertBack(word);
          }
      }
    import std.conv: to;
    string word = _words.front;
    _words.removeFront;
    w = to!W(word);
  }
  void iterableRead(W)(ref W w)
  {
    foreach(ref element; w)
      read(element);
  }
  void tupleRead(W)(ref W w)
  {
    static foreach(j; 0 .. W.length)
      read(w[j]);
  }
  void structRead(W)(ref W readStruct)
  {
    static foreach(fieldName; FieldNameTuple!W)
      {
        with(readStruct)
          {
            alias fieldSymbol = mixin(W.stringof, ".", fieldName);
            alias typeOfField = typeof(mixin(fieldName));
            static if (hasUDA!(fieldSymbol, If))
              enum condition = getUDAs!(fieldSymbol, If)[0].condition;
            else
              enum condition = "true";

            if (mixin(condition))
              {
                static if (hasUDA!(fieldSymbol, Dim))
                  {
                    static assert(isDynamicArray!(typeOfField)
                                  , text(W.stringof, ".", fieldName, " has Dim UDA but isn't a Dynamic Array."));
                    enum uda = getUDAs!(fieldSymbol, Dim)[0];
                    static assert(getArrayLevel!(typeOfField) == uda.levels,
                                  text(W.stringof, ".", fieldName, " has a Dim UDA with different number of dimensions than it's type."));
                    mixin(fieldName) = mixin(q{makeSlice!(baseType!typeOfField)(}, uda.dimensions, q{)});
                  }
                else static if (isDynamicArray!typeOfField && !is(typeOfField == string))
                  {
                    pragma(msg, "Warning: You probably want to add dimension to input ", W.stringof, ".", fieldName);
                  }
                read(mixin(fieldName));
              }
          }
      }
  }
  static foreach(i; 0 .. T.length)
    {
      static if (isBasicType!(T[i]) || is(T[i] == string))
        basicTypeRead(t[i]);
      else static if (is(typeof({foreach(ref e; t[i]){}})))
        iterableRead(t[i]);
      else static if (isTuple!(T[i]))
        tupleRead(t[i]);
      else static if (isAggregateType!(T[i]))
        structRead(t[i]);
    }
}

auto next(T)()
{
  T t;
  read(t);
  return t;
}

size_t ind(T)(T t)
{
  return cast(size_t)t;
}

ref auto at(R, I)(ref R r, I i)
{
  return r[cast(size_t)i];
}

void set(R, I, V)(ref R r, I i, V v)
{
  r[cast(size_t)i] = v;
}

import std.conv;
import std.traits;
import std.meta;
int _loglevel = -1;
void _log(string functionName, argSeq...)()
{
  foreach(i; 0 .. _loglevel * 4)
    write(' ');
  write(functionName);
  static foreach(arg; argSeq)
    {
      write(" ", arg.stringof, " = ", arg);
    }
  writeln;
}
enum logCall =
  q{
    debug
    {
     {
     _loglevel++;
     alias _parameterTuple = ParameterIdentifierTuple!(mixin(split(__FUNCTION__, ".").back));
     static if (_parameterTuple.length > 0)
       mixin(`_log!(__FUNCTION__, `, [_parameterTuple].joiner(", ").array, `);`);
     else
       _log!(__FUNCTION__);
      }
     foreach(i; 0 .. _loglevel * 4) write(' ');
     writeln('{');
     scope(exit)
       {
         foreach(i; 0 .. _loglevel * 4) write(' ');
         writeln('}');
         _loglevel--;
       }
    }
};

void logSym(S...)(int line = __LINE__, string file = __FUNCTION__)
{
  debug
    {
      foreach(i; 0 .. _loglevel * 4) write(' ');
      write(file, "(", line, "):");
      static foreach(s; S)
        write(" ", s.stringof, " = ", s);
      writeln;

    }
}

Array!E makeArray(E, S)(S size, lazy E value)
{
  auto a = Array!E();
  a.length = cast(size_t) size;
  foreach(ref ai; a)
    ai = value();
  return a;
}
Array!E makeArray(E, S)(S size)
{
  auto a = Array!E();
  a.length = cast(size_t) size;
  return a;
}

template mem(alias F)
{
  ReturnType!F[Tuple!(Parameters!F)] table;
  ReturnType!F mem(Parameters!F args)
  {
    return table.require(tuple(args), F(args));
  }
}

string input(string names, string type = "long")
{
  return text(type, " ", names, "; read(", names, ");");
}

auto pmod(T, W)(T t, W w)
{
  return (t%w + w) % w;
}

void rep(S, F)(F f, S times)
{
  static if (is(typeof(f(i))))
    foreach(i; 0 .. times)
      f(i);
  else
    foreach(i; 0 .. times)
      f();
}

void multi()
{
  foreach(tc; 0 .. next!long)
    next!TestCase.solve(tc);
}

void single()
{
  auto tc = next!TestCase;
  tc.solve;
}

void main()
{
  type;
}

import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.bigint, std.numeric, std.math, std.random;
import core.bitop;

string FMT_F = "%.10f";

static File _f;
void file_io(string fn) { _f = File(fn, "r"); }
static string[] s_rd;
T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }
T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }
T[] RDA(T = long)(T fix = 0) { if (_f.isOpen)  return _RDA!T(_f, fix); else return _RDA!T(fix); }

size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }

void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }

long mod = 10^^9 + 7;
//long mod = 998_244_353;
//long mod = 1_000_003;
void moda(T)(ref T x, T y) { x = (x + y) % mod; }
void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(T)(ref T x, T y) { x = (x * y) % mod; }
void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); }
void modd(T)(ref T x, T y) { x.modm(y.modpow(mod - 2)); }

void main()
{
	auto t = RD!int;
	auto ans = new long[](t);
	foreach (ti; 0..t)
	{
		auto n = RD;
		auto k = RD;

		ans[ti] = long.max;
		for (long i = 1; i*i <= n; ++i)
		{
			if (n % i == 0)
			{
				if (i <= k)
				{
					ans[ti].chmin(n / i);
				}
				if (n / i <= k)
				{
					ans[ti].chmin(i);
				}
			}
		}
	}

	foreach (e; ans)
		writeln(e);
	stdout.flush;
	debug readln;
}


import std.algorithm;
import std.conv;
import std.range;
import std.stdio;

void main ()
{
	int n, k;
	long s;
	while (readf (" %s %s %s", &n, &k, &s) > 0)
	{
		if (s < k || (n - 1) * 1L * k < s)
		{
			writeln ("NO");
			continue;
		}
		writeln ("YES");
		int pos = 1;
		int dir = +1;
		int [] ans;
		foreach (i; 0..k)
		{
			int len = (dir > 0) ? (n - pos) : (pos - 1);
			len = min (len, s - (k - i - 1)).to !(int);
			assert (len > 0);
			s -= len;
			pos += len * dir;
			ans ~= pos;
			if (pos == 1 || pos == n)
			{
				dir *= -1;
			}
		}
		writefln ("%(%s %)", ans);
	}
}

import core.bitop;
import std.algorithm;
import std.array;
import std.bitmanip;
import std.container;
import std.conv;
import std.functional;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
import std.range.interfaces;

void main()
{
    int n, k; long s;
    readf("%s %s %s", &n, &k, &s);
    readln;

    if (k > s || cast(long)(n-1) * k < s) {
        writeln("NO");
        return;
    }

    writeln("YES");
    auto ans = [1];
    while (k--) {
        auto step = cast(int) min(n-1, s - k);
        s -= step;
        if (ans.back + step <= n) ans ~= ans.back + step;
        else ans ~= ans.back - step;
    }

    ans.dropOne.writefln!("%(%s %)");
}

import core.bitop;
import std.algorithm;
import std.array;
import std.bitmanip;
import std.container;
import std.conv;
import std.functional;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
import std.range.interfaces;

void main()
{
    int n, k; long s;
    readf("%s %s %s", &n, &k, &s);
    readln;

    if (cast(long)(n-1) * k < s) {
        writeln("NO");
        return;
    }

    writeln("YES");
    auto ans = [1];
    while (k--) {
        auto step = cast(int) min(n-1, s - k);
        s -= step;
        if (ans.back + step <= n) ans ~= ans.back + step;
        else ans ~= ans.back - step;
    }

    ans.dropOne.writefln!("%(%s %)");
}