Problem:
Score : 300 points

Problem Statement

Snuke has an integer sequence A of length N.

He will freely choose an integer b.
Here, he will get sad if A_i and b+i are far from each other.
More specifically, the sadness of Snuke is calculated as follows:

abs(A_1 - (b+1)) + abs(A_2 - (b+2)) + ... + abs(A_N - (b+N))

Here, abs(x) is a function that returns the absolute value of x.

Find the minimum possible sadness of Snuke.

Constraints

1 \leq N \leq 2 \times 10^5

1 \leq A_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

Print the minimum possible sadness of Snuke.

Sample Input 1

5
2 2 3 5 5

Sample Output 1

2

If we choose b=0, the sadness of Snuke would be abs(2-(0+1))+abs(2-(0+2))+abs(3-(0+3))+abs(5-(0+4))+abs(5-(0+5))=2.
Any choice of b does not make the sadness of Snuke less than 2, so the answer is 2.

Sample Input 2

9
1 2 3 4 5 6 7 8 9

Sample Output 2

0

Sample Input 3

6
6 5 4 3 2 1

Sample Output 3

18

Sample Input 4

7
1 1 1 1 2 3 4

Sample Output 4

6

Solution in Rust (1.15.1) (judge verdict: Accepted)

// ternary operation
#[allow(unused_macros)]
macro_rules! _if {
  ($_test:expr, $_then:expr, $_else:expr) => {
    if $_test { $_then } else { $_else }
  };
  ($_test:expr, $_pat:pat, $_then:expr, $_else:expr) => {
    match $_test {
      $_pat => $_then,
      _     => $_else
    }
  };
}

use std::io::{ stdin, stdout, BufWriter, Write, BufReader };
use my::WriteWithDelim;

fn sadness_value3(ary: &[i64], b: i64) -> i64 {
  let x = ary.iter().enumerate().fold(0, | acc, (i, a) | {
    let i = i as i64 + 1;
    acc + (a - (b + i)).abs()
  });
  x
}

fn find_sv3(ary: &[i64], mut b: i64, mut prev: i64, add: i64) -> i64 {
  loop {
    let sv = sadness_value3(ary, b);
    if prev < sv {
      break;
    }
    prev = sv;
    b += add;
  }
  prev
}

fn arc100_a() {
  let stdin = stdin();
  let mut reader = my::ByteReader::with_capacity(16, BufReader::new(stdin.lock()));

  let stdout = stdout();
  let mut writer = BufWriter::new(stdout.lock());
  let mut i2b = my::Int2Byte::with_capacity(8);

  let vec = reader.read_int_vec_h::<i64>();

  let mut v = vec.iter().enumerate().map(| (i, a) | {
    (a - (i as i64 + 1))
  }).collect::<Vec<_>>();
  v.sort();
  let b = v[v.len() / 2];

  let zero = sadness_value3(&vec, b);
  let mut plus = sadness_value3(&vec, b + 1);
  if zero == 0 {
    writer.write_lf(i2b.from_i64(zero));
    writer.flush().unwrap();
    return;
  }

  if zero == plus {
    plus = sadness_value3(&vec, b + 2);
  }

  let ans = _if!(zero > plus,
    find_sv3(&vec, b + 2, plus, 1),
    find_sv3(&vec, b - 1, zero, -1));

  writer.write_lf(i2b.from_i64(ans));
  writer.flush().unwrap();
}

fn main() {
  arc100_a();
}

//------------------------------------------------------------------------
mod my {

  #[allow(dead_code)]
  pub fn intersection<T>(mut x: (T, T), y: (T, T)) -> Option<(T, T)>
    where T: PartialOrd +
             Copy +
             Debug
  {
    if x.0 >= y.1 || x.1 <= y.0 {
      return None;
    }

    if x.0 < y.0 {
      x.0 = y.0;
    }
    if x.1 > y.1 {
      x.1 = y.1;
    }

    Some(x)
  }

  //----------------------------------------------------------------------
  #[allow(dead_code)]
  pub fn rotate_right(vec: &mut Vec<u8>) {
    if vec.len() <= 1 {
      return;
    }

    let len = vec.len() - 1;
    let tmp = vec[len];
    for i in 0..len {
      vec[len - i] = vec[len - i - 1];
    }
    vec[0] = tmp;
  }

  #[allow(dead_code)]
  pub fn rotate_left(vec: &mut Vec<u8>) {
    if vec.len() <= 1 {
      return;
    }

    let len = vec.len() - 1;
    let tmp = vec[0];
    for i in 0..len {
      vec[i] = vec[i + 1];
    }
    vec[len] = tmp;
  }

  //----------------------------------------------------------------------
  #[allow(dead_code)]
  pub fn to_ascii_lowercase(mut b: u8) -> u8 {
    if b >= b'A' && b <= b'Z' {
      b += 0x20;
    }
    b
  }

  #[allow(dead_code)]
  pub fn to_ascii_uppercase(mut b: u8) -> u8 {
    if b >= b'a' && b <= b'z' {
      b -= 0x20;
    }
    b
  }

  #[allow(dead_code)]
  pub fn is_ascii_alphabetic(b: u8) -> bool {
    _if!(b >= b'A' && b <= b'A' || b >= b'a' && b <= b'z',
      true,
      false)
  }

  //----------------------------------------------------------------------
  macro_rules! pub_fn_Int2Byte_from_ty {
    (uint, $($name:ident => $_type:ty),*) => {$(
      #[allow(dead_code)]
      pub fn $name(&mut self, mut i: $_type) -> &[u8] {
        self.buf.clear();
        if i == 0 {
          self.buf.push(b'0');
          return self.buf.as_slice();
        }

        while i > 0 {
          self.buf.push((i % 10) as u8 + b'0');
          i /= 10;
        }
        self.buf.reverse();
        self.buf.as_slice()
      }
    )*};

    (int, $($name:ident => $_type:ty),*) => {$(
      #[allow(dead_code)]
      pub fn $name(&mut self, mut i: $_type) -> &[u8] {
        self.buf.clear();
        if i == 0 {
          self.buf.push(b'0');
          return self.buf.as_slice();
        }

        let mut negative = false;
        if i.is_negative() {
          negative = true;
          i = i.abs();
        }

        while i > 0 {
          self.buf.push((i % 10) as u8 + b'0');
          i /= 10;
        }
        if negative {
          self.buf.push(b'-');
        }
        self.buf.reverse();
        self.buf.as_slice()
      }
    )*};
  }

  #[derive(Debug)]
  pub struct Int2Byte {
    buf: Vec<u8>,
  }

  impl Int2Byte {
    #[allow(dead_code)]
    pub fn new() -> Self {
      Int2Byte {
        buf: Vec::new(),
      }
    }

    #[allow(dead_code)]
    pub fn with_capacity(capa: usize) -> Self {
      Int2Byte {
        buf: Vec::with_capacity(capa),
      }
    }

    pub_fn_Int2Byte_from_ty!(
      uint,
      from_usize => usize,
      from_u8    => u8,
      from_u16   => u16,
      from_u32   => u32,
      from_u64   => u64
    );

    pub_fn_Int2Byte_from_ty!(
      int,
      from_isize => isize,
      from_i8    => i8,
      from_i16   => i16,
      from_i32   => i32,
      from_i64   => i64
    );
  } // impl Int2Byte

  //----------------------------------------------------------------------
  use std::io::{ BufRead, BufWriter, Write };
  use std::fmt::Debug;
  use std::ops::{ Add, Sub, Mul, Neg, Rem };
  use std::str::{ self, FromStr };

  #[allow(dead_code)]
  pub fn gcd<T>(x: T, y: T) -> T
    where T: Rem<Output=T> +
             Default +
             PartialEq +
             Copy +
             Debug
  {
    _if!(y == T::default(),
      x,
      gcd(y, x % y))
  }

  pub trait WriteWithDelim {
    fn write_delim(&mut self, buf: &[u8], delim: &[u8]) -> usize;
    fn write_sp(&mut self, buf: &[u8]) -> usize;
    fn write_lf(&mut self, buf: &[u8]) -> usize;
  }

  impl<W> WriteWithDelim for BufWriter<W>
    where W: Write
  {
    #[allow(dead_code)]
    fn write_delim(&mut self, buf: &[u8], delim: &[u8]) -> usize
    {
      self.write(buf).unwrap() +
      self.write(delim).unwrap()
    }

    #[allow(dead_code)]
    fn write_sp(&mut self, buf: &[u8]) -> usize
    {
      self.write_delim(buf, b" ")
    }

    #[allow(dead_code)]
    fn write_lf(&mut self, buf: &[u8]) -> usize
    {
      self.write_delim(buf, b"\n")
    }
  } // impl<W> WriteWithDelim for BufWriter<W>

  //----------------------------------------------------------------------
  const SP: u8 = b' ';
  const LF: u8 = b'\n';

  #[allow(dead_code)]
  #[derive(Debug)]
  pub enum Direction {
    Horizontal,
    Vertical
  }

  #[derive(Debug)]
  pub struct ByteReader<R> {
    reader: R,
    buf   : Vec<u8>,
  }

  impl<R> ByteReader<R>
    where R: BufRead
  {
    #[allow(dead_code)]
    pub fn new(reader: R) -> Self {
      ByteReader {
        reader: reader,
        buf   : Vec::new(),
      }
    }

    #[allow(dead_code)]
    pub fn with_capacity(capa: usize, reader: R) -> Self {
      ByteReader {
        reader: reader,
        buf   : Vec::with_capacity(capa),
      }
    }

    //--------------------------------------------------------------------
    #[allow(dead_code)]
    pub fn read_1byte(&mut self) -> Option<u8> {
      self.buf.resize(1, 0);
      _if!(self.reader.read(&mut self.buf).unwrap() != 0,
        Some(self.buf[0]),
        None)
    }

    //--------------------------------------------------------------------
    fn read_until(&mut self, delim: u8) -> usize {
      self.buf.clear();
      self.reader.read_until(delim, &mut self.buf).unwrap()
    }

    #[allow(dead_code)]
    pub fn read_until_lf(&mut self) -> &[u8] {
      self.read_until(LF);
      self.buf.as_slice()
    }

    #[allow(dead_code)]
    pub fn read_until_sp(&mut self) -> &[u8] {
      self.read_until(SP);
      self.buf.as_slice()
    }

    //--------------------------------------------------------------------
    #[allow(dead_code)]
    pub fn read_byte_until_lf(&mut self) -> Vec<u8> {
      self.read_until(LF);
      let len = self.buf.len() - 1;
      if self.buf[len] == LF {
        self.buf.truncate(len);
      }
      self.buf.clone()
    }

    #[allow(dead_code)]
    pub fn read_string_until_lf(&mut self) -> String {
      unsafe { String::from_utf8_unchecked(self.read_byte_until_lf()) }
    }

    //--------------------------------------------------------------------
    fn parse_int<T>(&self) -> T
      where T: Add<Output=T> +
               Sub<Output=T> +
               Mul<Output=T> +
               Neg<Output=T> +
               Default +
               From<u8> +
               Debug
    {
      let len = self.buf.len();
      let mut i = 0;
      let mut n = T::default();
      let mut minus = false;

      if self.buf[i] == b'-' {
        minus = true;
        i += 1;
      } else if self.buf[i] == b'+' {
        i += 1;
      }

      while i < len && b'0' <= self.buf[i] && self.buf[i] <= b'9' {
        n = (n * T::from(10)) + T::from(self.buf[i] - b'0');
        i += 1;
      }

      _if!(minus,
        n.neg(),
        n)
    }

    fn parse_uint<T>(&self) -> T
      where T: Add<Output=T> +
               Sub<Output=T> +
               Mul<Output=T> +
               Default +
               From<u8> +
               Debug
    {
      let len = self.buf.len();
      let mut i = 0;
      let mut n = T::default();

      if self.buf[i] == b'+' {
        i += 1;
      }

      while i < len && b'0' <= self.buf[i] && self.buf[i] <= b'9' {
        n = (n * T::from(10)) + T::from(self.buf[i] - b'0');
        i += 1;
      }
      n
    }

    //--------------------------------------------------------------------
    #[allow(dead_code)]
    pub fn read_int_until_delim<T>(&mut self, delim: u8) -> T
      where T: Add<Output=T> +
               Sub<Output=T> +
               Mul<Output=T> +
               Neg<Output=T> +
               Default +
               From<u8> +
               Debug
    {
      self.read_until(delim);
      self.parse_int()
    }

    // s -> n
    #[allow(dead_code)]
    pub fn read_int_until_lf<T>(&mut self) -> T
      where T: Add<Output=T> +
               Sub<Output=T> +
               Mul<Output=T> +
               Neg<Output=T> +
               Default +
               From<u8> +
               Debug
    {
      self.read_int_until_delim(LF)
    }

    // s -> n
    #[allow(dead_code)]
    pub fn read_int_until_sp<T>(&mut self) -> T
      where T: Add<Output=T> +
               Sub<Output=T> +
               Mul<Output=T> +
               Neg<Output=T> +
               Default +
               From<u8> +
               Debug
    {
      self.read_int_until_delim(SP)
    }

    //--------------------------------------------------------------------
    #[allow(dead_code)]
    pub fn read_uint_until_delim<T>(&mut self, delim: u8) -> T
      where T: Add<Output=T> +
               Sub<Output=T> +
               Mul<Output=T> +
               Default +
               From<u8> +
               Debug
    {
      self.read_until(delim);
      self.parse_uint()
    }

    // s -> n
    #[allow(dead_code)]
    pub fn read_uint_until_lf<T>(&mut self) -> T
      where T: Add<Output=T> +
               Sub<Output=T> +
               Mul<Output=T> +
               Default +
               From<u8> +
               Debug
    {
      self.read_uint_until_delim(LF)
    }

    // s -> n
    #[allow(dead_code)]
    pub fn read_uint_until_sp<T>(&mut self) -> T
      where T: Add<Output=T> +
               Sub<Output=T> +
               Mul<Output=T> +
               Default +
               From<u8> +
               Debug
    {
      self.read_uint_until_delim(SP)
    }

    //--------------------------------------------------------------------

    fn from_utf8_unchecked(&mut self) -> &str {
      unsafe { str::from_utf8_unchecked(&self.buf) }
    }

    fn parse_tok<T>(&mut self) -> T
      where T: FromStr + Debug,
            <T as FromStr>::Err: Debug
    {
      self.from_utf8_unchecked()
        .trim()
        .parse()
        .unwrap()
    }

    // s -> n
    #[allow(dead_code)]
    pub fn parse_until_delim<T>(&mut self, delim: u8) -> T
      where T: FromStr + Debug,
            <T as FromStr>::Err: Debug
    {
      self.read_until(delim);
      self.parse_tok()
    }

    // s -> n
    #[allow(dead_code)]
    pub fn parse_until_lf<T>(&mut self) -> T
      where T: FromStr + Debug,
            <T as FromStr>::Err: Debug
    {
      self.parse_until_delim(LF)
    }

    // s -> n
    #[allow(dead_code)]
    pub fn parse_until_sp<T>(&mut self) -> T
      where T: FromStr + Debug,
            <T as FromStr>::Err: Debug
    {
      self.parse_until_delim(SP)
    }

    //--------------------------------------------------------------------
    #[allow(dead_code)]
    pub fn read_int_vec<T>(&mut self, n: usize, dir: Direction) -> Vec<T>
      where T: Add<Output=T> +
               Sub<Output=T> +
               Mul<Output=T> +
               Neg<Output=T> +
               Default +
               From<u8> +
               Debug
    {
      let delim = _if!(dir, Direction::Horizontal, SP, LF);
      let mut vec = Vec::with_capacity(n);
      for _ in 0..n - 1 {
        vec.push(self.read_int_until_delim(delim));
      }
      vec.push(self.read_int_until_delim(LF));
      vec
    }

    #[allow(dead_code)]
    pub fn read_uint_vec<T>(&mut self, n: usize, dir: Direction) -> Vec<T>
      where T: Add<Output=T> +
               Sub<Output=T> +
               Mul<Output=T> +
               Default +
               From<u8> +
               Debug
    {
      let delim = _if!(dir, Direction::Horizontal, SP, LF);
      let mut vec = Vec::with_capacity(n);
      for _ in 0..n - 1 {
        vec.push(self.read_uint_until_delim(delim));
      }
      vec.push(self.read_uint_until_delim(LF));
      vec
    }

    // horizontal
    //
    // N
    // s1 s2 s3, ..., sN -> [n1, n2, n3, ..., nN]
    #[allow(dead_code)]
    pub fn read_int_vec_h<T>(&mut self) -> Vec<T>
      where T: Add<Output=T> +
               Sub<Output=T> +
               Mul<Output=T> +
               Neg<Output=T> +
               Default +
               From<u8> +
               Debug
    {
      let n: usize = self.read_uint_until_lf();
      self.read_int_vec(n, Direction::Horizontal)
    }

    // horizontal
    //
    // N
    // s1 s2 s3, ..., sN -> [n1, n2, n3, ..., nN]
    #[allow(dead_code)]
    pub fn read_uint_vec_h<T>(&mut self) -> Vec<T>
      where T: Add<Output=T> +
               Sub<Output=T> +
               Mul<Output=T> +
               Default +
               From<u8> +
               Debug
    {
      let n: usize = self.read_uint_until_lf();
      self.read_uint_vec(n, Direction::Horizontal)
    }

    #[allow(dead_code)]
    pub fn read_uint_vec2<T>(&mut self, n: usize, m: usize) -> Vec<Vec<T>>
      where T: Add<Output=T> +
               Sub<Output=T> +
               Mul<Output=T> +
               Default +
               From<u8> +
               Debug
    {
      let mut vec = Vec::with_capacity(n);
      for _ in 0..n {
        vec.push(self.read_uint_vec(m, Direction::Horizontal));
      }
      vec
    }

    // N M
    // s11 s12 s13 ... s1M
    // s21 s22 s23 ... s2M
    // s31 s32 s33 ... s3M
    //   :
    // sN1 sN2 sN3 ... sNM
    // -> [
    //      [n11, n12, n13, ..., n1M],
    //      [n21, n22, n23, ..., n2M],
    //      [n31, n32, n33, ..., n3M],
    //          :
    //      [nN1, nN2, nN3, ..., nNM],
    //    ]
    #[allow(dead_code)]
    pub fn read_int_vec2<T>(&mut self, n: usize, m: usize) -> Vec<Vec<T>>
      where T: Add<Output=T> +
               Sub<Output=T> +
               Mul<Output=T> +
               Neg<Output=T> +
               Default +
               From<u8> +
               Debug
    {
      let mut vec = Vec::with_capacity(n);
      for _ in 0..n {
        vec.push(self.read_int_vec(m, Direction::Horizontal));
      }
      vec
    }

  } // impl<R> ByteReader<R>

} // mod my