Go: package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
	"strings"
)

func main() {
	io := NewIo()
	defer io.Flush()
	seq := make([]int, 360)
	N := io.NextInt()
	for i := 0; i < N; i++ {
		M, D, V, S := io.NextInt(), io.NextInt(), io.NextInt(), io.NextInt()
		M = (M - 1) * 30
		D--
		for j := M + D; j < M+D+V; j++ {
			seq[j%360] = intMax(S, seq[j%360])
		}
		for j := 0; j < S; j++ {
			seq[(M+D-j+360)%360] = intMax(S-j, seq[(M+D-j+360)%360])
			seq[(M+D+V+j-1)%360] = intMax(S-j, seq[(M+D+V+j-1)%360])
		}
	}
	ans := 360
	for _, val := range seq {
		ans = intMin(val, ans)
	}
	io.Println(ans)
}

func intMin(a, b int) int {
	if a > b {
		return b
	}
	return a
}

func intMax(a, b int) int {
	if a < b {
		return b
	}
	return a
}

// I/O template
type Io struct {
	reader    *bufio.Reader
	writer    *bufio.Writer
	tokens    []string
	nextToken int
}

func NewIo() *Io {
	return &Io{
		reader: bufio.NewReader(os.Stdin),
		writer: bufio.NewWriter(os.Stdout),
	}
}

func (io *Io) Flush() {
	err := io.writer.Flush()
	if err != nil {
		panic(err)
	}
}

func (io *Io) NextLine() string {
	var buff []byte
	for {
		line, isPrefix, err := io.reader.ReadLine()
		if err != nil {
			panic(err)
		}
		buff = append(buff, line...)
		if !isPrefix {
			break
		}
	}
	return string(buff)
}

func (io *Io) Next() string {
	for io.nextToken >= len(io.tokens) {
		line := io.NextLine()
		io.tokens = strings.Fields(line)
		io.nextToken = 0
	}
	s := io.tokens[io.nextToken]
	io.nextToken++
	return s
}

func (io *Io) NextInt() int {
	i, err := strconv.Atoi(io.Next())
	if err != nil {
		panic(err)
	}
	return i
}

func (io *Io) NextFloat() float64 {
	i, err := strconv.ParseFloat(io.Next(), 64)
	if err != nil {
		panic(err)
	}
	return i
}

func (io *Io) Println(a ...interface{}) {
	fmt.Fprintln(io.writer, a...)
}

func (io *Io) Printf(format string, a ...interface{}) {
	fmt.Fprintf(io.writer, format, a...)
}

func (io *Io) PrintIntSlice(a []int) {
	b := []interface{}{}
	for _, x := range a {
		b = append(b, x)
	}
	io.Println(b...)
}

func (io *Io) PrintStringSlice(a []string) {
	b := []interface{}{}
	for _, x := range a {
		b = append(b, x)
	}
	io.Println(b...)
}

func Log(name string, value interface{}) {
	fmt.Fprintf(os.Stderr, "%s=%+v\n", name, value)
}

func ExampleIo() {
	io := NewIo()
	defer io.Flush()
	a := make([]int, 0, 3)
	a[0], a[1], a[2] = io.NextInt(), io.NextInt(), io.NextInt()
	io.Println(a)
	io.PrintIntSlice(a)
	Log("io", a)
}

C++: #include <bits/stdc++.h>
using namespace std;
/*{{{*/  //template
#define rep(i,n) for(int i=0;i<n;i++)
constexpr int INF = numeric_limits<int>::max()/2;
constexpr long long LINF = numeric_limits<long long>::max()/3;
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
#define all(v) (v).begin(),(v).end()
#define sz(x) (int)(x).size()
#define debug(x) cerr<<#x<<":"<<x<<endl
#define debug2(x,y) cerr<<#x<<","<<#y":"<<x<<","<<y<<endl
//struct fin{ fin(){ cin.tie(0); ios::sync_with_stdio(false); } } fin_;
struct Double{ double d; explicit Double(double x) : d(x){} };
ostream& operator<<(ostream& os,const Double x){ os << fixed << setprecision(20) << x.d; return os; }
template<typename T> ostream& operator<<(ostream& os,const vector<T>& vec){ os << "["; for(const auto& v : vec){ os << v << ","; } os << "]"; return os; }
template<typename T,typename U> ostream& operator<<(ostream& os,const pair<T,U>& p){ os << "(" << p.first << ","<< p.second <<")"; return os; }
template<typename T> ostream& operator<<(ostream& os,const set<T>& st){ os<<"{"; for(T v:st) os<<v<<","; os <<"}"; return os; }
template<typename T,typename U> inline void chmax(T &x,U y){ if(y>x) x = y; }
template<typename T,typename U> inline void chmin(T &x,U y){ if(y<x) x = y; }
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
ll gcd(ll a,ll b){ if(b==0) return a; else return gcd(b,a%b); }
//constexpr double eps = 1e-14; 
constexpr double eps = 1e-10; 
constexpr ll mod = 1e9+7;
const int dx[]={1,0,-1,0} ,dy[] = {0,1,0,-1};
/*}}}*/

int N;
int a[5000];

int main(){
    cin >> N;
    rep(loop,N){   //100
        int m,d,v,s;
        cin >> m >> d >> v >> s;
        m--;
        d--;
        int st = m*30+d;
        int ed = st + v - 1;


        if(ed < 360){
            for(int i=0;i<st;i++){
                int n = 2000;
                chmin(n,abs(st-i));
                chmin(n,abs(i+360-ed));
                a[i] = max(a[i],s-n);
            }
            for(int i=st;i<=ed;i++) chmax(a[i],s);
            for(int i=ed+1;i<360;i++){
                int n = 2000;
                chmin(n,abs(i-ed));
                chmin(n,abs(st+360-i));
                a[i] = max(a[i],s-n);
            }
        }else{
            ed %= 360;
            for(int i=0;i<=ed;i++) chmax(a[i],s);
            for(int i=ed+1;i<st;i++){
                int n = 2000;
                chmin(n,abs(i-ed));
                chmin(n,abs(st-i));
                a[i] = max(a[i],s-n);
            }
            for(int i=st;i<360;i++) chmax(a[i],s);
        }
    }

    int mx = 2000;
    for(int i=0;i<360;i++) chmin(mx,a[i]);
    //rep(i,360) cout << i << " "<< a[i] << endl;
    cout << mx << endl;

}

C++: #include <bits/stdc++.h>

using namespace std;

#define INF 1001000100010001000
#define int long long 
#define rep(i, N) for (int i = 0; i < N; i++)
#define Int(x) int x; cin >> x;
#define int2(x, y) Int(x); Int(y);
#define vi vector<int>
#define vvi vector< vi >

signed main()
{
    int n;
    cin >> n;

    vvi day(n, vi(361, -1));

    rep(i, n) {
        //cout << endl;
        //cout << i << endl;
        int2(a, b);
        int2(d, c);
        rep(j, d) {
            day[i][(j + ((a - 1) * 30) + b - 1) % 360] = c;
            //cout << day[i][(j + (a * 30) + b) % 360] << " ";
        }
        int st = (a - 1) * 30 + b - 1;
        int en = st + d - 1;
        rep(j, 360) {
            int dist = min( (st - j + 360) % 360,
                    (j - en + 360) % 360);
            day[i][j] = max(day[i][j], max(c - dist, 0LL));
            //cout << day[i][j] << " ";
        }
        //cout << endl;
    }

    int ans = INF;
    rep(j, 360) {
        int tmp = 0;
        rep(i, n) {
            tmp = max(tmp, day[i][j]);
        }
        ans = min(ans, tmp);
    }

    cout << ans << endl;

    return 0;
}

C++: #include<deque>
#include<queue>
#include<vector>
#include<algorithm>
#include<iostream>
#include<set>
#include<cmath>
#include<tuple>
#include<string>
#include<chrono>
#include<functional>
#include<iterator>
#include<random>
#include<unordered_set>
#include<unordered_map>
#include<array>
#include<map>
#include<iomanip>
//<< setprecision(5)
using namespace std;
typedef long long int llint;
typedef long double lldo;
#define mp make_pair
#define mt make_tuple
#define pub push_back
#define puf push_front
#define pob pop_back
#define pof pop_front
#define fir first
#define sec second
#define res resize
#define ins insert
#define era erase
#define dme cout<<-1<<endl;return 0
const int mod=1000000007;
const int big=1e9;
const long double pai=3.141592653589793238462643383279502884197;
const long double eps=1e-7;
template <class T,class U>void mineq(T& a,U b){if(a>b){a=b;}}
template <class T,class U>void maxeq(T& a,U b){if(a<b){a=b;}}
llint gcd(llint a,llint b){if(a%b==0){return b;}else return gcd(b,a%b);}
llint lcm(llint a,llint b){return a/gcd(a,b)*b;}
int main(void){
	int n,i,j,ans=999999;
	cin>>n;
	vector<tuple<int,int,int>> x(n);//?§??????????v,Si
	for(i=0;i<n;i++){
		int m,d,s,v;cin>>m>>d>>v>>s;
		x[i]=mt(m*30+d,v,s);
	}
	for(i=0;i<360;i++){
		int gen=0;
		for(j=0;j<n;j++){
			int p=(3600+i-get<0>(x[j]))%360;//??£????????????????????\??????
			int v=get<1>(x[j]);
			int s=get<2>(x[j]);
			if(p<v){maxeq(gen,s);}
			else{
				maxeq(gen,s+v-p-1);
				maxeq(gen,s+p-360);
			}
		}
		mineq(ans,gen);
	}
	cout<<ans<<endl;
	return 0;
}